0
how can I calculate the rest of a division using only add and subtraction?
5 Answers
+ 7
Add or substract (depending on the sign) the denominator from the numerator till you have a number between 0 (included) and the denominator (not included).
+ 2
You can define a function like this:
def divide(n,d):
rem = 0
quotient = 0
while True:
if n < d:
rem = n
break
else:
n -= d
quotient += 1
return quotient, rem
Full code available here: https://code.sololearn.com/cLPbxIoU77mN
+ 1
Example:
14 % 5:
14 - 5 = 9
9 isn't between 0 and 5, so we loop.
9 - 5 = 4
4 is between 0 and 5 and is the rest we are looking for.
0
example please...
0
thanks