Write a C++ program to calculate the sum of two numbers.
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.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #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: