C++ bootcamp · Lab 02

Sum two numbers

easyInput8 minLesson: User Input

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read two integers and print their sum.

std::cin >> skips whitespace, so the two numbers may be on one line or two — it reads them the same way.

Input. Two integers separated by space or newline.

Output. One integer: a + b

Constraints

  • -1000000 ≤ a, b ≤ 1000000

Examples

Example 1
Input
3 5
Output
8
Example 2 — Negatives included.
Input
10 -4
Output
6
Hint
  1. int a, b; std::cin >> a >> b;
  2. Print a + b followed by "\n".
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>

int main() {
  int a, b;
  std::cin >> a >> b;
  std::cout << a + b << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.