Menu Close

C++ Program To Add Two Numbers

CPP program to add two numbers

In this tutorial, we are going to learn How to write a C++ or CPP program to add two numbers.
Here we will take two ways to add two numbers, In the first ways, we will ask for two numbers from the user and in the second ways we already defined two values into variables.

In the previous tutorial, we have seen a simple C++ program to print “Hello World on the screen.

Here we will see two different examples to add two numbers in C++.

Table of Contents

CPP Program To Add Two Numbers

Example 1:

In this example, we will ask two numbers from users and store them inside two variables and then add both two numbers and display on the screen.

#include<iostream>
using namespace std;

int main()
{

int first_number, second_number, result;

//Take first number from user
cout<<"Enter First Number:- ";
cin>>first_number;

// Take second number from user
cout<<"Enter Second Number:- ";
cin>>second_number;

//Add first_number and second_number and store result variable
result = first_number + second_number;


//Display result on the screen
cout<<"Addition of "<<first_number<<" and "<<second_number<<" is:- "<<result;

return 0;

}

Output

Enter First Number:- 56
Enter Second Number:- 87
Addition of 56 and 87 is:- 143

Explain above code:

In above program

  • we have declared three variables first_number, second_number, and result.
  • Ask two numbers from the user and store them inside first_number and second_number variables.
  • Add both first_number and second_number variables values and store result variables.
  • Display value on the screen.

Example 2:

In this example, we will assign two numbers inside two different variables and then add both numbers.

#include<iostream>
using namespace std;

int main()
{

int first_number = 120, second_number = 240, result;


cout<<"The first Number is:- "<<first_number;

cout<<"\nThe second number is:- "<<second_number;

//Add first_number and second_number and store result variable
result = first_number + second_number;


//Display result on the screen
cout<<"\nAddition of "<<first_number<<" and "<<second_number<<" is:- "<<result;

return 0;
}

Output

The first number is:- 120
The second number is:- 240
Addition of 120 and 240 is:- 360

Explain above code:

  • To add two numbers in C++. we have declared three variables first_number, second_number, and result.
  • Ask two numbers from the user and store them inside first_number and second_number variables.
  • Add both first_number and second_number variables values and store result variables.
  • Display value on the screen.

Conclusion:

In this tutorial, you have learned how to write a C++ or CPP program to add two numbers using two different ways.
I hope this add two numbers in C++ example article will help you. If you like this article, please share comment and share with your friends who want to write this type of C++ example program.

For reference:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply