Add two numbers without using addition operator | 4 methods

Given two numbers, add them without using addition operator.

 


 
Method 1: Use subtraction operator
 

 
 
Method 2: Repeated Addition/Subtraction using –/++ operator
 

Download   Run Code

Output:

13 -3 3

 
Method 3: Using printf()
 

This method make use of two facts –
 
1. We can use an asterisk (*) to pass the width precision to printf(), rather than hard coding it into the format string
 
2. printf() function return the number of characters printed on output stream.

We can also use %*c, ‘ ‘ in place of %*s, “”.

 
Method 4: Half adder logic
 

Please note that method 3 only works for non-negative numbers, while method 1, 2 and 4 can work for any integer.

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂