Loading...
C++ Program to Add Two Numbers

C++ Program to Add Two Numbers

In this example, we will learn how to add two integers entered by a user using C++ cout statement.

Program to Add Two Numbers

In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen.


Example: Program to Add Two Numbers

#include <iostream>
using namespace std;

int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;
    
    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;

    // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;

    // Prints sum 
    cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;     

    return 0;
}

Output

Enter two integer: 2
2
2 + 2 = 4

Working of above Program

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

Then, the variables firstNumber and secondNumber are added using + operator and stored in sumOfTwoNumbers variable. Finally, sumOfTwoNumbers is displayed on the screen.


Next Example

We hope that this Example helped you develop better understanding of the concept of Program to Add Two Numbers in C++.

Keep Learning : )

In the next Example, we will learn about C++ Find Quotient and Remainder.


- Related Topics