Open In App

Implement *, - and / operations using only + arithmetic operator

Last Updated : 23 Mar, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

Given two numbers, perform multiplication, subtraction, and division operations on them, using '+' arithmetic operator only.
 


Operations can be performed as follows: 

Subtraction :-  a - b = a + (-1)*b.
Multiplication :- a * b = a + a + a ... b times.
Division :- a / b =  continuously subtract b from a and 
                  count how many times we can do that.


The above steps look simple, but it is slightly challenging as we can't even use - to subtract. 

C++
Java Python3 C# PHP JavaScript

Output:  

Subtraction is 6
Product is -54
Division is 4

Time Complexity: O(max(|a|, |b|)), Where flipSign() function is O(|a|), sub() function is O(|b|), mul() function is O(max(|a|, |b|)) and division() function is O(|a/b|), Thus Overall, the time complexity of the code is O(max(|a|, |b|)).

Space Complexity: O(1), as it does not use any additional data structures.


Related Articles : 
 


If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
 


Next Article
Practice Tags :

Similar Reads