8 different ways to Add Two Numbers in C/C++ - GeeksforGeeks

29 May, 2021
Given two numbers A and B, the task is to find the sum of the given two numbers.Examples: 
Input: A = 5, B = 6 Output: sum = 11Input: A = 4, B = 11 Output: sum = 15  
Method 1 – using Addition Operator: Here simply use the addition operator between two numbers and print the sum of the number. 
sum = A + B 
Below is the implementation of the above approach: 
C++
// C++ program to add two number// using addition operator#include <iostream>using namespace std;// Function to return sum// of two numberint addTwoNumber(int A, int B){    // Return sum of A and B    return A + B;} // Driver Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    cout << "sum = " << addTwoNumber(A, B);    return 0;}
sum = 15
Method 2 – using Subtraction Operator: Here simply use the subtraction operator between two numbers, two times so that minus-minus multiply and produce + operator and return the sum of the number.  
sum = A – (-B)  
Below is the implementation of the above approach: 
C++
// C++ program to add two number// using subtraction operator#include <iostream>using namespace std;// Function to return sum// of two numberint addTwoNumber(int A, int B){    // Return sum of A and B    return A - (-B);} // Driver Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    cout << "sum = " << addTwoNumber(A, B);    return 0;}
sum = 15
Method 3 – using increment/decrement operator: Here use increment/decrement operator while one number decrement to zero and in another number increment by one when the first number decrement by one, return the second number. 
while(B > 0) { A++; B–; }  
Below is the implementation of the above approach: 
C++
// C++ program to add two number using// increment/decrement operator#include <iostream>using namespace std; // Function to return sum// of two numberint 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 Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    cout << "sum = " << addTwoNumber(A, B);    return 0;}
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++
// C++ program to add two number// using printf method#include <iostream>using namespace std;// Function to return sum// of two numberint addTwoNumber(int A, int B){    // Return sum of A and B    return printf("%*s%*s", A, "", B, "");} // Driver Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    printf("sum = %d", addTwoNumber(A, B));    return 0;}
               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++
// C++ program to add two number// using half adder method#include <iostream>using namespace std; // Function to return sum// of two numberint 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 Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    printf("sum = %d", addTwoNumber(A, B));    return 0;}
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++
// C++ program to add two number// using log and exponential#include <bits/stdc++.h>using namespace std; // Function to return sum// of two numberint addTwoNumber(int A, int B){    // Return sum of A and B    return log(exp(A) * exp(B));} // Driver Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    printf("sum = %d", addTwoNumber(A, B));    return 0;}
sum = 15
Method 7 – using Recursion: 
Get the numbers A and B whose sum is to be calculated.Base Case: If A is greater than 0, then return B.
Get the numbers A and B whose sum is to be calculated.
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++
// C++ program to add two number// using Recursion#include <iostream> // Function to return sum// of two numberint addTwoNumber(int A, int B){    // Base Case    if (!A)        return B;     // Recursive Call    else        return addTwoNumber((A & B) << 1, A ^ B);} // Driver Codeint main(){    // Given two number    int A = 4, B = 11;     // Function call    printf("sum = %d", addTwoNumber(A, B));    return 0;}
sum = 15
Method 8 –  using this pointer
Taking two numbers x and y from the user and the task is to find the sum of the given two numbers using this pointer.
Example:
input:
Enter Two Number: 5
4
output:
Sum is: 9
This can be done by accessing the data member of an object by using this pointer and performing an addition operation between them.
C++
// CPP program for above approach#include <iostream>using namespace std; class A{    int a, b, sum; public:    A(int x, int y)    {        a = x;        b = y;    }         // Using this pointer to    // access variable    void calcSum()    {      sum = this->a + this->b;    }     void showSum()    {      cout << "Sum is: " << sum << endl;    }}; // Driver Codeint main(){    int x, y;    cout << "Enter Two Numbers: ";    x = 4, y = 5;    A a(x, y);    a.calcSum();    a.showSum();    return 0;}
Enter Two Numbers: Sum is: 9
aroraprajjwal
Code_Mech
C Programs
C++ Programs
Mathematical
Recursion
School Programming
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C Program to read contents of Whole File
Producer Consumer Problem in C
C program to find the length of a string
Exit codes in C/C++ with Examples
Difference between break and continue statement in C
C++ Program for QuickSort
C++ program for hashing with chaining
Sorting a Map by value in C++ STL
cin in C++
delete keyword in C++