The WP Debugging plugin must have a wp-config.php file that is writable by the filesystem.

C++ Program to Add Two Numbers

C++ Program to Add Two Numbers

In this article, we will learn about the addition of two numbers in C++. we will show different approaches to adding two numbers. All these methods do the same thing, that is to add two numbers given by the user or take it on our own. Let’s discuss the C++ Program to add two numbers.

Algorithm to addition of two numbers in C++

  • Start
  • Declare variables A, B, and sum
  • Read values for A and B
  • Add the values of A and B and assigned the result to a sum variable
  • Print sum
  • Stop

Method 1: Addition of two numbers in C++ using the arithmetic addition(+) operator

In this method, we will see a C++ program to add two numbers using the plus(+) arithmetic operator.

Code Implementation of addition of two numbers in C++ using arithmetic addition:

#include<iostream>
using namespace std;
int main()
{
    int A = 10, B = 20, sum;
    
    sum = A + B;
    cout << "Sum of two number " << A <<" and " << B << " = " << sum;
    
    return 0;
}
Output:
The sum of two numbers 10 and 20 = 30

Method 2: Addition of two numbers in C++ using a user-defined function.

In this method, we will see a C++ program to add two numbers in which the user allows to insert the values of two numbers using a user-defined function.

Code Implementation of addition of two numbers in C++ using a user-defined function:

#include<iostream>
using namespace std;
int main()
{
	int A, B, sum;	
	cout << "Please enter the First Number  : "<<endl;
	cin >> A;	
	cout << "Please enter the Second Number : "<<endl;
	cin >> B;	
	sum = A + B;
	cout << "Sum of Two Numbers " << A <<" and " << B << " = " << sum;	
	return 0;
}
Output:
Please enter the First Number: 10
Please enter the Second Number: 20
Sum of Two Numbers 10 and 20 = 30

Method 3: Addition of two numbers in C++ using functions

In this method, we will create a function that accepts two arguments and return the sum of those two arguments. We will call that function inside out the main() function.

Code Implementation of addition of two numbers in C++ using functions:

#include<iostream>
using namespace std;

int add(int x, int y)
{
	return x + y;
}

int main()
{
	int A, B, sum;
	
	cout << "Please enter the First Number  : "<<endl;
	cin >> A;
	
	cout << "Please enter the Second Number : "<<endl;
	cin >> B;
	
	sum = add(A, B);
	cout << "Sum of Two Numbers " << A <<" and " << B << " = " << sum;
	
	return 0;
}
Output:
Please enter the First Number: 10
Please enter the Second Number: 10
Sum of Two Numbers 10 and 10 = 20

Method 4: Addition of two numbers in C++ using recursion

In this method, we will use a recursive function to apply the addition of two numbers. In which the function calls itself from its definition part.

Code implementation of addition of two numbers in C++ using recursion:

#include<iostream>
using namespace std;

int addFun(int, int);
int main()
{
    int A, B, sum;
    cout<<"Enter Two Numbers: ";
    cin>>A>>B;
    sum = addFun(A, B);
    cout<<"\nResult = "<<sum;
    cout<<endl;
    return 0;
}
int addFun(int a, int b)
{
    if(b==0)
        return a;
    else
        return (1+addFun(a, b-1));
}
Output:
Enter Two Numbers: 20 20
Result = 40

Time Complexity: O(1)
Space Complexity: O(1)

Conclusion

This blog taught us the different approaches to how to add two numbers in C++. For beginners, you must have to practice more coding questions like adding two numbers as these questions help in building logic as well as making your coding stronger. We hope this article will help you to enhance your knowledge and give you a good grasp of C++ programming language. Many companies like TCS, Wipro, Capgemini, and many others ask these basic questions in their interview process. Also, you can check our MYCODE platform to practice more questions on data structures and algorithms.

Other C++ Programs

C++ Program to Reverse a Number
C++ program for heap sort
Bubble Sort in C++ with Program Code and Example

Leave a Reply

Your email address will not be published. Required fields are marked *