C++ Program to Add Two Numbers

In this article, you will learn and get code about the addition of any two numbers given by the user (at run-time). Here is the list of programs. All of these programs perform the same function, which is to add two numbers entered by the user, but they do so in different ways:

Add two numbers of the integer type

Let's first start with a very simple program for the addition of two numbers. The dry run of this program is given just after the output.

#include<iostream>
using namespace std;

int main()
{
    int num1, num2, add;
    cout<<"Enter Two Numbers: ";
    cin>>num1>>num2;
    add = num1+num2;
    cout<<"\nResult = "<<add;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is the sample run:

C++ program to add two numbers

Now supply any two numbers as input, say 50 and 60. Press ENTER to see the output of the addition of the given two numbers, as shown in the screenshot below:

add two numbers in cpp

Now let's take a look at the brief explanation of the code along with a dry run using 10 and 20 as input.

What if the user enters real numbers?

The above program is correct only if the user enters the two numbers as integer values. That is, a value that does not contain any decimal parts. But what if the user supplies any value that contains a decimal part? In other words, what if the user enters real numbers? The solution to this problem is given in the following program.

#include<iostream>
using namespace std;

int main()
{
    float num1, num2, add;
    cout<<"Enter Two Numbers: ";
    cin>>num1>>num2;
    add = num1+num2;
    cout<<"\nResult = "<<add;
    cout<<endl;
    return 0;
}

As you can see from the above program, To add any two real numbers, just change the data type to float (floating-point type) before declaring all the three variables, say num1, num2, and add. so that these variables become able to handle floating-point values (values that contain decimal parts). Here is a sample run of the above program:

add two real numbers cpp

Add two numbers in C++ using a function

Let's use this approach to create a program that adds two numbers given by the user. The output of this program is totally the same as the output of the very first program given over here.

#include<iostream>
using namespace std;

int addFun(int, int);
int main()
{
    int num1, num2, add;
    cout<<"Enter Two Numbers: ";
    cin>>num1>>num2;
    add = addFun(num1, num2);
    cout<<"\nResult = "<<add;
    cout<<endl;
    return 0;
}
int addFun(int a, int b)
{
    return (a+b);
}

Add two numbers in C++ using recursion

This program uses a recursive function to apply the addition of two numbers. A recursive function is one that calls itself from its definition part.

#include<iostream>
using namespace std;

int addFun(int, int);
int main()
{
    int num1, num2, add;
    cout<<"Enter Two Numbers: ";
    cin>>num1>>num2;
    add = addFun(num1, num2);
    cout<<"\nResult = "<<add;
    cout<<endl;
    return 0;
}
int addFun(int a, int b)
{
    if(b==0)
        return a;
    else
        return (1+addFun(a, b-1));
}

Add two numbers in C++ using the friend function

The question is: write a program in C++ that uses the friend function to add two numbers entered by the user. The answer to this question is given below.

#include<iostream>
using namespace std;

class CodesCracker
{
    private:
        int a, b, sum;
    public:
        void getData()
        {
            cout<<"Enter Two Numbers: ";
            cin>>a>>b;
        }
        void showResult()
        {
            cout<<"\nResult = "<<sum;
        }
        friend void add(CodesCracker &c);
};
int main()
{
    CodesCracker c;
    c.getData();
    add(c);
    c.showResult();
    cout<<endl;
    return 0;
}
void add(CodesCracker &c)
{
    c.sum = (c.a) + (c.b);
}

In C++, use Class to add two numbers

This is the last program of this article; here we have applied the class approach to create a program in C++ for the addition of two given numbers.

#include<iostream>
using namespace std;

class CodesCracker
{
    private:
        int x, y;
    public:
        void getData();
        int add();
};
void CodesCracker::getData()
{
    cout<<"Enter Two Numbers: ";
    cin>>x>>y;
}
int CodesCracker::add()
{
    return (x+y);
}
int main()
{
    CodesCracker c;
    int sum;
    c.getData();
    sum = c.add();
    cout<<"\nResult = "<<sum;
    cout<<endl;
    return 0;
}

Same Program in Other Language

You might enjoy the following program in another programming language:

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!