C++ Program to Add Two Numbers

 

Algorithm of Adding two Numbers

1 Step: START.

2 Step: Initialize integers A, B and C.

3 Step: Accept two integers A and B from User.

3 Step: Now do the operation using formula C=A+B.

4 Step: Now the addition is stored in C Variable.

5 Step: Print C.

6 Step: STOP.

Now we will do this arithmetic operation in C++. There are different methods to make an Addition in C++ we will see it one by one.

1. C++ Program to Add Two Numbers

In this program, we will ask the user to enter the two numbers and do addition using formula. This addition will store in the third Variable and at the end, the value stored in the third variable is printed.

C++

Output:

c++ program to add two numbers

In the above program, when the compiler asks the user to enter two integers, the two integers are stored in the variables firstNumber and secondNumber.

These two integers are added using + operator and their addition is stored in the third variable sumOfTwoNumbers.

Finally, the output is printed on the screen.

2. Addition Using Class and Function in C++

In this program we will do addition of two numbers using class and declare function for addition.

C++

Output:

c++ program to add two numbers

Note: In the above program the sum() function from the main is not called because the sum() function is called from the show() function.

3. C++ Program to Add Two Numbers Using Function Overloading

The Function Overloading is an ability to create multiple functions which can have the same name but with the different implementations. Function Overloading is also called as Method Overloading.

In this program, we will declare three functions with the same name but their operations will be different.

C

Output:

addition of two numbers in c++

4. C++ Addition Program Using Class

In this program, we will declare one class to do the addition of two integers.

C++

Output:

addition of two numbers in c++

In the above program, we have declared class Maths to perform the addition of two numbers.

No comments