← Back

20. Sum of Two Integers

Easy

📝 Problem

Get 2 integer inputs and display their sum.

Input Format:
Accept two integers as input
Output Format:
Print the sum as follows:
"Sum of NUM1 and NUM2 is SUM"
Constraints:
1 ≤ N1, N2 ≤ 10^15
Sample Input 1:
78 22
Sample Output 1:
Sum of 78 and 22 is 100
Sample Input 2:
23 45
Sample Output 2:
Sum of 23 and 45 is 68
💡 Key Concepts

What You'll Learn:

  • Multiple Input Reading: How to read two space-separated integers using cin
  • Long Long Type: Why use long long for large integers (up to 10^15)
  • Stream Insertion Chaining: Combining multiple values with << operator
  • Formatted Output: Creating a complete sentence with variables embedded in text

Why Long Long?

The constraint (up to 10^15) exceeds the range of int (up to ~2×10^9). Using long long ensures we can handle values up to approximately 9×10^18.

How cin Handles Whitespace:

The cin operator automatically skips whitespace (spaces, tabs, newlines) when reading numbers. This means cin >> a >> b works whether the input is "78 22" or "78\n22".

💻 Solution Code
#include <iostream>

using namespace std;

int main() {
    long long a, b;
    cin >> a >> b;
    cout << "Sum of " << a << " and " << b << " is " << a + b;
    return 0;
}