# 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
• Add two Numbers of Floating-point Type
• Add two Numbers using user-defined Function
• Add two Numbers using Recursion
• Add two Numbers using Friend Function
• Add two Numbers using Class

## 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()
{
cout<<"Enter Two Numbers: ";
cin>>num1>>num2;
cout<<endl;
return 0;
}```

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

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:

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

• Three variables namely num1, num2, add of type int gets declared
• These variables hold only integer type value. That is, a whole number. A number, that does not contains any decimal part
• Now ask from user to enter any two numbers
• Get and store these two numbers in variables num1 and num2
• Now just initialize num1+num2 to the variable add
• Finally print the value of add as output
• For example, if user enters 10 and 20 as input
• Then, 10 gets stored in num1 and 20 gets stored in num2
• Using num1+num2, 10+20 or 30 gets initialized to add
• And 30 is the addition result of 10 and 20. Thats it

### 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()
{
cout<<"Enter Two Numbers: ";
cin>>num1>>num2;
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 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 main()
{
cout<<"Enter Two Numbers: ";
cin>>num1>>num2;
cout<<endl;
return 0;
}
{
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 main()
{
cout<<"Enter Two Numbers: ";
cin>>num1>>num2;
cout<<endl;
return 0;
}
{
if(b==0)
return a;
else
}```

## 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;
}
};
int main()
{
CodesCracker c;
c.getData();
c.showResult();
cout<<endl;
return 0;
}
{
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();
};
void CodesCracker::getData()
{
cout<<"Enter Two Numbers: ";
cin>>x>>y;
}
{
return (x+y);
}
int main()
{
CodesCracker c;
int sum;
c.getData();
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 »