Find the division remainder of a number

PythonInteger Division

Python Problem Overview


How could I go about finding the division remainder of a number in Python?

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

Python Solutions


Solution 1 - Python

you are looking for the modulo operator:

a % b

for example:

>>> 26 % 7
5

Of course, maybe they wanted you to implement it yourself, which wouldn't be too difficult either.

Solution 2 - Python

The remainder of a division can be discovered using the operator %:

>>> 26%7
5

In case you need both the quotient and the modulo, there's the builtin divmod function:

>>> seconds= 137
>>> minutes, seconds= divmod(seconds, 60)

Solution 3 - Python

26 % 7 (you will get remainder)

26 / 7 (you will get divisor, can be float value)

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

Solution 4 - Python

If you want to get quotient and remainder in one line of code (more general usecase), use:

quotient, remainder = divmod(dividend, divisor)
#or
divmod(26, 7)

Solution 5 - Python

From Python 3.7, there is a new math.remainder() function:

from math import remainder
print(remainder(26,7))

Output:

-2.0  # not 5

Note, as above, it's not the same as %.

Quoting the documentation:

> math.remainder(x, y) > > Return the IEEE 754-style remainder of x with > respect to y. For finite x and finite nonzero y, this is the > difference x - n*y, where n is the closest integer to the exact value > of the quotient x / y. If x / y is exactly halfway between two > consecutive integers, the nearest even integer is used for n. The > remainder r = remainder(x, y) thus always satisfies abs(r) <= 0.5 * > abs(y). > > Special cases follow IEEE 754: in particular, remainder(x, math.inf) > is x for any finite x, and remainder(x, 0) and remainder(math.inf, x) > raise ValueError for any non-NaN x. If the result of the remainder > operation is zero, that zero will have the same sign as x. > > On platforms using IEEE 754 binary floating-point, the result of this > operation is always exactly representable: no rounding error is > introduced.

Issue29962 describes the rationale for creating the new function.

Solution 6 - Python

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

26 - (26 // 7 * 7) = 5

Solution 7 - Python

Use the % instead of the / when you divide. This will return the remainder for you. So in your case

26 % 7 = 5

Solution 8 - Python

We can solve this by using modulus operator (%)

26 % 7 = 5;

but 26 / 7 = 3 because it will give quotient but % operator will give remainder.

Solution 9 - Python

Modulo would be the correct answer, but if you're doing it manually this should work.

num = input("Enter a number: ")
div = input("Enter a divisor: ")

while num >= div:
    num -= div
print num

Solution 10 - Python

You can find remainder using modulo operator Example

a=14
b=10
print(a%b)

It will print 4

Solution 11 - Python

If you want the remainder of your division problem, just use the actual remainder rules, just like in mathematics. Granted this won't give you a 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 make this in a calculator format, just substitute valone = 8 with valone = int(input("Value One")). Do the same with valtwo = 3, but different vairables obviously.

Solution 12 - Python

Here's an integer version of remainder in Python, which should give the same results as C's "%" operator:

def remainder(n, d):
    return (-1 if n < 0 else 1) * (abs(n) % abs(d))

Expected results:

remainder(123, 10)   ==  3
remainder(123, -10)  ==  3
remainder(-123, 10)  == -3
remainder(-123, -10) == -3

Solution 13 - Python

you can define a function and call it remainder with 2 values like rem(number1,number2) that returns number1%number2 then create a while and set it to true then print out two inputs for your function holding number 1 and 2 then print(rem(number1,number2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobView Question on Stackoverflow
Solution 1 - PythonUku LoskitView Answer on Stackoverflow
Solution 2 - PythontzotView Answer on Stackoverflow
Solution 3 - PythonKarush MahajanView Answer on Stackoverflow
Solution 4 - PythonAlok NayakView Answer on Stackoverflow
Solution 5 - PythonChris_RandsView Answer on Stackoverflow
Solution 6 - PythonalysView Answer on Stackoverflow
Solution 7 - PythonBender the GreatestView Answer on Stackoverflow
Solution 8 - PythonKATTAMURU ANILView Answer on Stackoverflow
Solution 9 - PythonCooperView Answer on Stackoverflow
Solution 10 - PythonSwarnima ShishodiaView Answer on Stackoverflow
Solution 11 - PythonS. AhmadView Answer on Stackoverflow
Solution 12 - PythoncalamariView Answer on Stackoverflow
Solution 13 - Pythonuser13790410View Answer on Stackoverflow