C++ Program to Add Two Numbers



In this article, we'll show how to add two numbers using C++ and display the result. Adding two numbers is a basic arithmetic operation, meaning we combine their values to get a total. We will see different methods to solve this.

Adding two numbers in C++

Different Ways to Add Two Numbers in C++

In C++, there are various approaches to add two numbers, each approach using its own cases and advantages. Below are the mentioned list:

Now, we will understand each approach with their examples.

Using Addition(+) Operator

This is simple plus operator(+) which add sum of two integers.

Example

This is the basic example that illustrate addition of two different integers.

#include <iostream>	
using namespace std;

int main()
{
   int a = 10;
   int b = 20;
   int sum;
   sum = a + b;
   cout << "Addition of a and b is " << sum << endl; 
   return 0;
}

The above code produces the following result:

Addition of a and b is 30

Using User Input

Take the input of two numbers, then use the addition operator to calculate their sum and display the result.

Example

In this C++ program, we will learn how to take an input from the users and display the sum as a result.

#include <iostream>
using namespace std;

int main()
{
   int a, b;
   int sum;
   cout << "Enter the first digit: ";
   cin >> a;
   cout << "Enter the second digit: ";
   cin >> b;
   sum = a + b;
   cout << "Addition of first and second digit is " << sum << endl; 
   return 0;
}

The above code produces the following result:

Enter the first digit: 5
Enter the second digit: 6
Addition of first and second digit is 11

Using Increment and Decrement Operators

The increment operator (++) adds 1 to n1 in each iteration, while the decrement operator (--) subtracts 1 from n2 until it reaches 0. This simplifies the process of adding two numbers.

Example

Below is the C++ program that illustrates the addition of two numbers using increment and decrement operators.

#include <iostream>
using namespace std;

int main() {
    int n1, n2;
    n1 = 10;
    n2 = 20;

    // Use increment and decrement operators to perform addition
    while (n2 != 0) {
        ++n1;  // Increment num1
        --n2;  // Decrement num2
    }

    // Display the result
    cout << "The sum is: " << n1 << endl;

    return 0;
}

The above code produces the following result.

The sum is: 30

Using Bitwise Operators

In Bitwise Operators, we use the bitwise AND (&), which calculates the common set bits of x and y, say (carry), the bitwise XOR (^) adds two integers (x and y) without the carry, and the left shift (<<) shifts the carry by one position to add it to x in the next iteration, with the loop continuing until there are no more carry bits to add.

Example

Following is the C++ program that illustrates addition of two number using Bitwise Operator.

#include <iostream>
using namespace std;

// Function to add two numbers using bitwise operators
int add(int x, int y) {
   while (y != 0) {
       // Carry now contains common set bits of x and y
       int carry = x & y;

       // Sum of bits of x and y where at least one of the bits is not set
       x = x ^ y;

       // Carry is shifted by one so that adding it to x gives the required sum
       y = carry << 1;
   }
   return x;
}

int main() {
   int x = 100;
   int y = 200;

   // Call the add() function
   int result = add(x, y);

   // Display the result
   cout << "The sum of " << x << " and " << y << " is " << result << endl;

   return 0;
}

The above code produces the following result:

The sum of 100 and 200 is 300

Using Recursion

A function calling itself is called recursion. Here, we create a custom function named sum() that accepts two parameters, a and b, and returns the result by using the + operator. Inside the main function, it is called with two integers to generate the result.

Example

In this C++ program, we add two numbers using recursion.

#include <iostream>
using namespace std;

// Define the sum() function
int sum(int a, int b) {
   return a + b; 
}

int main() {
   int num1 = 8;
   int num2 = 7;

   // Call the sum() function
   int result = sum(num1, num2);
   
   // Display the result
   cout << "The sum of " << num1 << " and " << num2 << " is " << result << endl;

    return 0;
}

The above code produces the following result.

The sum is: 30

Using log() and exp() Methods

In this approach, we create a global function named sum() that takes two parameters, x and y. It checks if either value is less than or equal to zero, returning NAN for invalid inputs. Then, it calculates the sum using logarithms with exp(log(x) + log(1 + (y / x))), where exp() calculates the exponential of an argument using base e (Euler's number), the inverse of log() function.

Example

Below the C++ program add two numbers using log() and exp() methods.

#include <iostream>
#include <cmath> 
using namespace std;

// Function to add two numbers using log() and exp()
double sum(double x, double y) {
    // Check for non-positive values
    if (x <= 0 || y <= 0) {
        cerr << "Error: Both numbers must be positive." << endl;
        return NAN; // Return Not-a-Number for invalid input
    }

    // Use log() and exp() to find the sum
    return exp(log(x) + log(1 + (y / x)));  
}

int main() {
    double x = 18;
    double y = 17;

    // Call the sum() function
    double result = sum(x, y);

    // Display the result
    cout << "The sum of " << x << " and " << y << " is " << result << endl;
    return 0;
}

The above code produces the following result.

The sum of 18 and 17 is 35

Conclusion

In this article, we learned different ways to add two numbers in C++, such as using the addition operator, taking input from the user, using increment and decrement operators, bitwise operators, recursion, and math functions like log() and exp(). Each method has its own benefits, giving us flexible options for adding numbers in C++.

Updated on: 2025-05-09T15:26:44+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements