C++ Program to add two Numbers (Integer/Decimal)

In this program, we will learn how to add two Numbers in C++ programming language.

To add two numbers in C++, we will ask the user to enter the two number and place the addition of the two number in sum variable of same type and print this variable in the output which is the sum of the two entered numbers as shown here in the following program.

Program to add two Integers

Code:-

#include <iostream>
 
using namespace std;
 
int main()
{
    int a, b, sum;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    sum = a + b;
    cout <<"Sum of the numbers: " << sum << endl;

    return 0;
}

Output:-

Enter first number: 45
Enter second number: 65
Sum of the numbers: 110

Program to add two decimal (float)

Just change the datatype of the variables in the above program to float.

#include <iostream>
 
using namespace std;
 
int main()
{
    float a, b, sum;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    sum = a + b;
    cout <<"Sum of the numbers: " << sum << endl;

    return 0;
}
Enter first number: 27.86
Enter second number: 78.42
Sum of the numbers: 106.28

You can learn about many other C++ Programs Here.







The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...