 # C++ Program to add two numbers

November 24, 2021, Learn eTutorial
1153

Here we are explaining how to write a C++ program to find the sum of two integer numbers

## How to find the sum of two numbers?

This is a computer program to asks the user to enter two integers. The entered integers are then stored in two different variables. These two variables are then added together by using the mathematical operator ‘+’ and the resultant value is stored in another variable. Finally, the variable's value, where the result is stored is displayed on the screen.

## How can we implement a C++ program to add two numbers?

C++ uses different header files to provide information necessary for the program. Here the basic input-output service file iostream is used.  A preprocessor directive` #include `is used to include the contents of the iostream file in our program.

Here we are using namespace std to write `cout, cin`, etc instead of `std::cin, std::cout.`

The program always starts with the main function. Within the main body declare 3 integer type variables to store the two numbers to be added and to store the value of the sum of the entered integer. ‘ num1, num2, sum’.Display a message to enter the numbers to be added using the object` cout`.

Read the numbers using the object `cin`. The first number is to the variable num1 and the second number is to the variable num2. Add the numbers using the arithmetic operator ‘+’. And store the result to the variable sum.sum=num1+num2.

Print the value of the variable sum on the screen using `cout.`Finally, exit the program.

### Algorithm

Step 1:  Call the header file` iostream`.

Step 2: Use the` namespace std.`

Step 3: Open the main function as an integer, `int main().`

Step 4: Declare integer type variables ; num1, num2, sum.

Step 5: Print a message on the screen to “Enter two numbers:” using` cout.`

Step 6: Read the first number to variable num1 and the second number to variable num2 using `cin.`

Step 7: Add the two numbers and store the result in the sum.
Sum=num1+num2.

Step 8: Display the value of the variable ‘sum ‘ on the screen using` cout`.

Step 9: Exit.

## C++ Source Code

```                                          ```#include <iostream>
using namespace std;

int main() {

int num1, num2, sum;

cout << "Enter two numbers: ";
cin >> num1>> num2;

// sum of two numbers in stored in variable sum
sum = num1+ num2;

// prints sum
cout << num1<< " + " <<  num2<< " = " << sum;

return 0;
}```
```

## OUTPUT

```Enter two numbers: 10
20
10 + 20 = 30```