+ 2

# integer division without *or/?

how can i do integer division without using * or/?

12 Answers

+ 2

You are welcome, sama baluom! Here's the code for positive numbers:
-------------------
#finding the quotient of the division a/b
a = 100
b = 7
quotient = 0
while a >= b:
a -= b
quotient += 1
print(quotient)
-------------------
Let me know if anything looks confusing :)

+ 2

I'm assuming you don't want to use // or divmod() or any other sneaky method...
Use the idea that division is repeated subtraction.
For positive numbers, use a loop to keep subtracting the divisor from the dividend until the remainder is smaller than the divisor. Use a counter to keep track of the number of times you had to subtract. The final value of the counter is the quotient.
Hope that helps :)

+ 2

+ 1

thanks!

+ 1

thanks a lot^^ its pretty clear now for me , but was wondering if I can use input so that I can change the value everytime..?

+ 1

why are you trying to do this(´・ω・｀)
well, there's a method faster than keep subtracting
take 12345/111 for example
get first 3 digits of 12345, which is 123
keep subtracting 111 and count until it's less than 111, and you will get 1 as first digit of quotion and rest 12
then plus the fourth digits after 12, which becomes 124
keep subtracting and count then get 1...13
by doing this, just like how we do with hand, will be fast if you don't want to use * or //

+ 1

sama baluom Sure! Modify it to
a = int(input())
b = int(input())
at the beginning.

+ 1

Flandre Scarlet! thanks for answering me but i couldn't understand how to code that ? , should I use slicing or what?

+ 1

thank you♡ waiting for it

+ 1

thank you;)

0

Flandre Scarlet haha! You adapted the method for integer long division! Nice!

0

sama baluom i'll write it at code playground, wait for a while😀