Actually i was trying for Positive numbers but the below provided code worked for negative numbers also.I am not able to ubderstand how it is working for negative numbers?
Recursive Multiplication
Hey @dare_devil_007 as long as b is positive this code will work irrespective of sign of a, because multiplication is essentially addition so if we are calculating a*b it is equivalent to adding a b times and similarly if we are calculating (-a)*b it is equivalent to adding -a, b times.
Recursive function written takes as input a and b and just adds a to itself b times since we do addition doesnt flip signs this code works for negative numbers as well.
it is even working for negative value of b,that is what i couldn’t able to unserstand it.Can you please run the code and check.
Hey i checked it and i think this might be because of online compiler/ide design since the code is not working on offline compiler.
so can you explain how to generalize my code for negative values too
If b is negative then print -1 * mul(a, -b) otherwise print mul(a , b)