Find the division of the remainder of a number

How can I find the remainder of number division in Python?

For example:
If the number is 26, and the divided number is 7, then the remainder of the division is 5.
(since 7 + 7 + 7 = 21 and 26-21 = 5.)

+69
python integer-division
Apr 7 2018-11-11T00:
source share
8 answers

You are looking for the modulo operator:

a % b 

eg:

 26 % 7 

Of course, perhaps they wanted you to implement it yourself, which would also be difficult.

+100
Apr 07 '11 at 16:45
source share

The rest of the division can be detected using the % operator:

 >>> 26%7 5 

If you need both a factor and a module, there is a built-in divmod function:

 >>> seconds= 137 >>> minutes, seconds= divmod(seconds, 60) 
+128
May 01 '11 at 11:49
source share

If you want to avoid modulation, you can also use a combination of four basic operations :)

 26 - (26 // 7 * 7) = 5 
+12
Jul 14 '13 at 3:07 on
source share

26% 7 (you will get the balance)

26/7 (you get a divisor could be a float value)

26 // 7 (you will only get the integer divisor value))

+8
Mar 17 '16 at 10:14
source share

Modulo will be the right answer, but if you do it manually, this should work.

 num = input("Enter a number: ") div = input("Enter a divisor: ") while num >= div: num -= div print num 
+4
Apr 7 '11 at 17:25
source share

I would suggest looking for modulo

+3
Apr 7 2018-11-11T00:
source share

Use instead of% / when you share. This will return the balance to you. So in your case

 26 % 7 = 5 
+2
Apr 07 2018-11-11T00:
source share

If you need the rest of the separation problem, just use the actual remainder rules, as in math. This will not give you decimal output.

 valone = 8 valtwo = 3 x = valone / valtwo r = valone - (valtwo * x) print "Answer: %s with a remainder of %s" % (x, r) 

If you want to do this in a calculator format, simply replace valone = 8 with valone = int(input("Value One")) . Do the same with valtwo = 3 , but obviously different wairabs.

0
Oct 19 '17 at 0:33
source share



All Articles