ComputerShikshak.com

C++ - Add Two Numbers

Problem Statement

Write a program in C++ to add two numbers entered by the user.

Brief Description

According to the problem statement we have to write a program in C++ that will take two numbers as input from the user through the keyboard and will add the two given numbers and will find the sum.

The two numbers given by the user may be integers like 3, 15, 23 etc. or may be floating point or real numbers like 6.9, 11.2, 22.1 etc. or may be the combination of both.

For example, if the user enters 8 and 4 as input, the program that adds two integer numbers will generate 12 as output since 8+4=12.

To view Flowchart click the link given below:

Flowchart – Add Two Numbers

Add Two Integer Numbers Using a Third Variable

In this case two integer numbers given by the user are added and the result is stored in a third variable. Finally the result is displayed along with an appropriate message.

Program

				
					#include<iostream>
using namespace std;
int main()
{
	int num1,num2,add;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	add=num1+num2;
	cout<<"Sum is: "<<add;
	return 0;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively.
  • The values stored in num1 and num2 are added and the result is stored in the variable add.

  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers Without using a Third Variable

In this case two integer numbers given by the user are added and the result is directly displayed (without storing in a third variable) along with an appropriate message.

Program

				
					#include<iostream>
using namespace std;
int main()
{
	int num1,num2;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	cout<<"Sum is: "<<num1+num2;
	return 0;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • 2 integer variables namely num1 and num2 are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively.
  • Finally the values stored in num1 and num2 are added and the result is directly displayed along with an appropriate message.

Add Two Floating Point or Real Numbers Using a Third Variable

In this case two floating point or real numbers given by the user are added and the result is stored in a third variable. Finally the result is displayed along with an appropriate message.

Program

				
					#include<iostream>
using namespace std;
int main()
{
	float num1,num2,add;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	add=num1+num2;
	cout<<"Sum is: "<<add;
	return 0;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following outputs:

Output 1

Output 2

Output 3

Explanation

In this program:
  • 3 float variables namely num1, num2 and add are declared.
  • The numbers entered by the user are stored in the variables num1 and num2 respectively.
  • The values stored in num1 and num2 are added and the result is stored in the variable add.

  • Finally the value of add is displayed along with an appropriate message.
Note:-
  • This program is mainly written to add two floating point or real numbers.
  • Though this program can also be used to add two integer numbers.
  • Except that this program can also be used to add an integer number with a floating point or real number.
  • If the user enters an integer number it will automatically be converted to a floating point or real number while storing the integer number to a float variable.
  • This program can be modified to add two floating point or real numbers without using a third variable.

Add Two Integer Numbers using a Function

In this case two integer numbers given by the user are added using a function and the result is displayed along with an appropriate message.

Program

				
					#include<iostream>
using namespace std;
int sum(int n1,int n2);
int main()
{
	int num1,num2,add;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	add=sum(num1,num2);
	cout<<"Sum is: "<<add;
	return 0;
}
int sum(int n1,int n2)
{
	int a;
	a=n1+n2;
	return a;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • Inside main() method 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function an integer variable namely a is declared.
  • The values stored in n1 and n2 are added and the sum is stored in a.
  • Value of a is returned to the main() function and is stored in the variable add.
  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers using a Function (Alternate Program-1)

A modified version of the program to “Add Two Integer Numbers using a Function” is as follows:

Program

				
					#include<iostream>
using namespace std;
int sum(int n1,int n2);
int main()
{
	int num1,num2,add;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	add=sum(num1,num2);
	cout<<"Sum is: "<<add;
	return 0;
}
int sum(int n1,int n2)
{
	return n1+n2;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • Inside main() method 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function the values stored in n1 and n2 are added and the sum is returned to the main() function and is stored in the variable add.
  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers using a Function (Alternate Program-2)

An another modified version of the program to “Add Two Integer Numbers using a Function” is as follows:

Program

				
					#include<iostream>
using namespace std;
int sum(int n1,int n2);
int main()
{
	int num1,num2;
	cout<<"Enter a number: ";
	cin>>num1;
	cout<<"Enter another number: ";
	cin>>num2;
	cout<<"Sum is: "<<sum(num1,num2);
	return 0;
}
int sum(int n1,int n2)
{
	return n1+n2;
}
				
			

Output

When the program given above is executed using Dev-C++ IDE we get the following output:

Explanation

In this program:
  • Inside main() method 2 integer variables namely num1 and num2 are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function the values stored in n1 and n2 are added and the sum is returned to the main() function and is directly displayed along with an appropriate message.
Share this page on
Scroll to Top