7 different ways to Add Two Numbers in C/C++

Given two numbers A and B, the task is to find the sum of the given two numbers.

Examples:

Input: A = 5, B = 6
Outut: sum = 11

Input: A = 4, B = 11
Output: sum = 15

Method 1 – using Addition Operator: Here simply use the addition operator between two number and print the sum of the number.



sum = A + B

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

Method 2 – using Substraction Operator: Here simply use subtraction operator between two number, two times so that minus-minus multiply and produce + operator and return sum of number.

sum = A – (-B)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

Method 3 – using increment/decrement operator: Here use increment/decrement operator while one number decrement to zero and in other number increment by one when first number decrement by one, return the second number.



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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

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

printf(“%*s%*s”, A, “”, B, “”);

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

Method 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.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at the same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.

Sum = A & B;
Carry = x ^ y

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



sum = 15

Method 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)));

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

Method 7 – using Recursion:

  1. Get the numbers A and B whose sum is to be calculated.
  2. Base Case: If A is greater than 0, then return B.
    if(A > 0) {
       return B;
    }
    
  3. Recursive Call: Update A to (A&B)<<1 and B to A ^ B and recursively call for the updated value.
    recursive_function((A & B) << 1, A ^ B);
    

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

sum = 15

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.