Add two numbers without using addition operator | 5 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

 

 

Method 5: Using logarithm and exponential function

 

 
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 🙂
 





Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
Daniel Sigurdsson
Guest
Daniel Sigurdsson

You have one more:

printf(“%gn”, log(exp(x)*exp(y)));

wpDiscuz