# 8 different ways to Add Two Numbers in C/C++

• Difficulty Level : Basic
• Last Updated : 21 Jun, 2022

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 = 11
Input: 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 ``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)

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 ``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)

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 ``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)

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 ``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)

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 ``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(logn)

Auxiliary Space: O(1)

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 ``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(logn)

Auxiliary Space: O(1)

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++

 `// C++ program to add two number``// using Recursion``#include ` `// 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(logn)

Auxiliary Space: O(logn)

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 ``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 Code``int` `main()``{``    ``int` `x, y;``    ``cout << ``"Enter Two Numbers: "``;``    ``x = 4, y = 5;``    ``A a(x, y);``    ``a.calcSum();``    ``a.showSum();``    ``return` `0;``}`

Output

`Enter Two Numbers: Sum is: 9`

Time complexity: O(1)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up