C++ Program to Add Two Numbers



Adding two numbers is a basic arithmetic operation. In programming, the addition of two numbers performs the task of two different integers by utilizing various approaches. In this article, we will illustrate how to add two numbers and display their sum on the screen.

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

Output

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

Output

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

Output

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

Output

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

Output

The above code produces the following result ?

The sum is: 30

Using log() and exp() Methods

In this approach, we create one global function named sum() that accepts two parameters ? x and y. Then it processed to check whether x or y is less than or equal to zero, returning NAN(not a number) for invalid input. Next, it uses exp(log(x) + log(1 + (y / x))) to operate the addition of two number using logarithm. The exp() function calculates the exponential value of an argument using the base e (Euler's number), which represents e raised to the power of that argument. This function appears as the inverse of the 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;
}

Output

The above code produces the following result ?

The sum of 18 and 17 is 35
Updated on: 2024-12-06T17:10:03+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements