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

`// 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*

*filter_none*

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

`// 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*

*filter_none*

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

`// 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*

*filter_none*

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

`// 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*

*filter_none*

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

`// 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*

*filter_none*

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

`// 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*

*filter_none*

**Output:**

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.if(A > 0) { return B; }

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

*filter_none*

**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.

## Recommended Posts:

- Add two integers of different base and represent sum in smaller base of the two
- Ways of dividing a group into two halves such that two elements are in different groups
- Ways to express a number as product of two different factors
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Ways to arrange Balls such that adjacent balls are of different types
- Different ways for Integer to String Conversions In Java
- Different ways of Method Overloading in Java
- Array of Strings in C++ (5 Different Ways to Create)
- Different ways of Reading a text file in Java
- Ways to color a skewed tree such that parent and child have different colors
- Different ways to initialize a variable in C/C++
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Number of ways to arrange K different objects taking N objects at a time
- Different ways to use Const with Reference to a Pointer in C++
- 3 Different ways to print Fibonacci series in Java
- Program to find the maximum difference between the index of any two different numbers
- Write a program to add two numbers in base 14

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.