codescracker
c++

C++ Program to Add Two Numbers



« Previous Program Next Program »

In this article, you will learn and get code about addition of any two number given by user (at run-time). Here are the list of programs. All these programs does the same job, that is to add two numbers given by user, but using different-different approach:

Add two Numbers of Integer Type

Let's first start with very simple program for the addition of two number. 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 build and run under Code::Blocks IDE. Here is the sample run:

C++ program to add two numbers

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

add two numbers in cpp

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

What if User enters Real Numbers ?

The above program is correct only when user supplies the two number input as an integer type value. That is, a value that does not contains any decimal part. But what if user supply any value that contains decimal part. In other words, what if 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 becomes able to handle floating-point value (value that contains decimal part). Here is the sample run of above program:

add two real numbers cpp

Add two Numbers in C++ using Function

Let's approach function to create a program that adds two given number by user. The output of this program is totally same as the output of very first program given over here. To learn more about function, refer to Function in C++ tutorial.

#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 recursive function to apply the addition of given two number. Recursive function, means a function 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 Friend Function

The question is, write a program in C++, that uses friend function to add two numbers entered by user. The answer to this question is given below. To learn more about friend function, then refer to its seperate tutorial Friend Function in C++

#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);
}

Add Two Numbers in C++ using Class

This is the last program of this article, here we have applied class approach to create a program in C++ for the addition of two given number. To learn more about class, refer to class in C++ tutorial.

#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 may like the same program in other programming languages:

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker