C++ Program to Add Two Numbers

In this example, you will learn to calculate the sum of two integers entered by the user using variables.

Example: Program to Add Two Integers

#include <iostream>
using namespace std;
int main() {
    int firstNumber, secondNumber, sum;

    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;
    // sum of two numbers in stored in variable sum
    sum = firstNumber + secondNumber;
    // Prints sum
    cout << firstNumber << " + " << secondNumber << " = " << sum;
    return 0;
}

Output

Enter two integers: 4
5
4 + 5 = 9

In this program, the user is asked to enter two integers. These two integers are stored in variables firstNumber and secondNumber respectively.

They are then added using the + operator and stored in the sum variable.

Finally, sum is displayed on the screen.