Open In App

Add Two Numbers in C++

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The addition of two numbers is a simple task in C++ that can be done using the arithmetic addition operator (+). But there are many other ways to find the sum of two numbers in C++. In this article, we will discuss 7 different ways to add two numbers in C++.

add numbers in c++

1. Using Addition Operator

Here simply use the addition operator between two numbers and print the sum of the number.

sum = A + B

C++ Program to Add Two Numbers Using the Addition Operator

C++




// C++ program to add two number
// using addition operator
#include <iostream>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return A + B;
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


Output

sum = 15
  • Time complexity: O(1)
  • Auxiliary Space: O(1)

2. Using the Subtraction Operator

Here simply use the subtraction operator between two numbers, two times so that minus-minus multiplies and produce the + operator, and return the sum of the numbers.

sum = A - (-B)

C++ Program to Add Two Numbers Using the Subtraction Operator

C++




// C++ program to add two number
// using subtraction operator
#include <iostream>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return A - (-B);
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


Output

sum = 15
  • Time complexity: O(1)
  • Auxiliary Space: O(1)

3. Using the Increment and Decrement Operators

Here use the increment/decrement operator to add two numbers. Decrement a number by one till it becomes zero and increment another number by one, return the second number.

while(B > 0){ 
    A++; 
    B--; 
}

C++ Program to Add Two Numbers Using the Increment and Decrement Operators

C++




// C++ program to add two number using
// increment/decrement operator
#include <iostream>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // When A is positive
    while (A > 0) {
        A--;
        B++;
    }
  
    // When A is negative
    while (A < 0) {
        A++;
        B--;
    }
  
    // Return sum of A and B
    return B;
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


Output

sum = 15
  • Time Complexity: O(N), where N is the min(A,B)
  • Auxiliary Space: O(1)

4. Using printf() method

Here “%*s” specifier print the value of a variable, the value of the variable times, and printf() method return how many character print on the screen.

printf("%*s%*s", A, "", B, ""); 

C++ Program to Add Two Numbers Using printf() Function

C++




// C++ program to add two number
// using printf method
#include <iostream>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return printf("%*s%*s", A, "", B, "");
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


Output

               sum = 15
  • Time complexity: O(1)
  • Auxiliary Space: O(1)

5. Using Half Adder Method

A sum of two bits can be obtained by performing Bitwise XOR(^) of the two bits. Carry bit can be obtained by performing Bitwise AND(&) of two bits. This is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic to integers.

Algorithm

  • Run a loop till B becomes 0.
  • Inside loop:
    • carry = A & B calculates the carry by performing a bitwise AND operation between A and B.
    • A = A ^ B calculates the sum of A and B without considering any carry. This operation uses bitwise XOR to add A and B without carrying.
    • B = carry << 1 calculates the carry for the next iteration by shifting the carry one position to the left. This is because, in the next iteration, the carry will need to be added to the sum.
  • Return A that holds the sum of two numbers.

C++ Program to Add Two Numbers Using the Half Adder Method

C++




// C++ program to add two number
// using half adder method
#include <iostream>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
  
    // Iterate till there is no carry
    while (B != 0) {
        // Carry now contains common
        // set bits of A and B
        int carry = A & B;
  
        // Sum of bits of A and B
        // where at least one of the
        // bits is not set
        A = A ^ B;
  
        // Carry is shifted by one so
        // that adding it to A gives
        // the required sum
        B = carry << 1;
    }
  
    return A;
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


Output

sum = 15
  • Time complexity: O(log n)
  • Auxiliary Space: O(1)

6. Using Exponential and Logarithm

The idea is to find the exponential of the given two numbers and print the logarithmic of the result.

printf("%g\n", log(exp(A) * exp(B)));

C++ Program to Add Two Numbers Using Exponential and Logarithm

C++




// C++ program to add two number
// using log and exponential
#include <bits/stdc++.h>
using namespace std;
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return log(exp(A) * exp(B));
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


Output

sum = 15
  • Time complexity: O(log n)
  • Auxiliary Space: O(1)

7. Using Recursion

Algorithm

// Base case: If A is greater than 0, then return B
if(A > 0)
   return B;

// Update A to (A&B)<<1 and B to A ^ B and recursively call for the updated value
else
   recursive_function((A & B) << 1, A ^ B);

C++ Program to Add Two Numbers Using Recursion

C++




// C++ program to add two number
// using Recursion
#include <iostream>
  
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Base Case
    if (!A)
        return B;
  
    // Recursive Call
    else
        return addTwoNumber((A & B) << 1, A ^ B);
}
  
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
  
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


Output

sum = 15
  • Time complexity: O(log n)
  • Auxiliary Space: O(log n)

Explanation

  • (A & B) << 1: This expression calculates the bitwise AND of A and B and then left-shifts the result by one position. This is done to carry over the bits that are set in both A and B.
  • A ^ B: This expression calculates the bitwise XOR of A and B. This is done to perform the addition without carrying.
  • The recursive call addTwoNumber((A & B) << 1, A ^ B); keeps adding the numbers until A becomes zero (base case). At each recursive step, the function calculates the carry bits and the non-carry bits separately and recursively calls itself with the new values.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads