How it works
a mod n is the remainder r when a is divided by n, chosen so that a = n × q + r with q a whole number. In maths, q is rounded down (floor), so for a positive n the remainder is always between 0 and n.
a mod n = a − n × floor(a ÷ n)
Many programming languages (C, Java, JavaScript’s %) round q towards zero instead, which gives a negative remainder when a is negative.
Worked example
17 mod 5: 17 = 5 × 3 + 2, so the answer is 2. −17 mod 5: floor(−17 ÷ 5) = −4, so r = −17 − 5 × (−4) = 3. In JavaScript, −17 % 5 gives −2.
Which convention do programming languages use?
Every language keeps the rule a = n × q + r. They differ in how they round the quotient q, and that decides the sign of r when a or n is negative.
| Convention | −17 mod 5 | 17 mod −5 | 7 mod −3 |
|---|---|---|---|
| Maths (floored), Python % | 3 | −3 | −2 |
| C %, JavaScript % | −2 | 2 | 1 |
Python's % gives a result with the same sign as the second number, n. C rounds the quotient toward zero, and JavaScript's % does the same, so their result takes the sign of a. Python's math.fmod follows C, not Python's own %.
This calculator uses the floored convention. It accepts a negative a or a negative n. When the two conventions disagree, it also shows the C and JavaScript answer below the result.
Worked example with a negative divisor
Find 7 mod −3. First, 7 ÷ (−3) = −2.33…, and rounding down gives q = −3. Then r = 7 − (−3) × (−3) = 7 − 9 = −2. With the floored rule the remainder takes the sign of n, so a negative n gives an answer between n and 0.
How do you find a remainder on an ordinary calculator?
- Divide: 250 ÷ 24 = 10.4166…
- Keep the whole-number part: q = 10.
- Multiply back and subtract: 250 − 24 × 10 = 10.
So 250 mod 24 = 10. For a negative a, round the quotient down (toward minus infinity), not toward zero.
Where mod is useful
- Even or odd: a mod 2 is 0 for even numbers and 1 for odd ones.
- Last digit: for a whole number a ≥ 0, a mod 10 is its last digit. 1234 mod 10 = 4.
- Days of the week: 100 days after a Monday, 100 mod 7 = 2, so it is a Wednesday.
- Hours: 250 hours after midnight is 10 whole days plus 250 mod 24 = 10 hours, so the time is 10:00.
This page finds a mod n only. Modular powers (ab mod n) and modular inverses are separate operations that it does not compute.
What happens with mod 0 in code?
The result is undefined in maths, and languages handle it differently. Python raises a ZeroDivisionError. In C the behavior is undefined. JavaScript returns NaN. This calculator shows an error message.
Tips
- Clock arithmetic is mod 12 (or 24): 9 hours after 8 o’clock is (8 + 9) mod 12 = 5.
- a mod n = 0 means n divides a exactly.
- Check: n × q + r should give back a.
Questions
What is −17 mod 5?
3 in the usual maths convention. Languages such as C, Java and JavaScript give −2 for -17 % 5.
Can the numbers be decimals?
Yes: 7.5 mod 2 = 1.5.
What is mod 0?
It’s undefined, because you can’t divide by zero.
What is 1234 mod 10?
4. 1234 = 10 × 123 + 4. For any whole number 0 or above, mod 10 gives the last digit.
What is 3 mod 4?
3. When a is between 0 and n (and smaller than n), n fits zero times, so a mod n = a.
Does Python give the same answer as this calculator?
Yes for the % operator: Python also gives the result the sign of n, so −17 % 5 is 3 and 7 % −3 is −2. Python's math.fmod follows C instead.
Sources
- Python Language Reference, Expressions. §6.7 Binary arithmetic operations (modulo sign rule, ZeroDivisionError). Checked 22 November 2026.
- Python Standard Library: math module. math.fmod (result has the sign of x). Checked 22 November 2026.
- ISO/IEC 9899:2024 working draft N3220 (C). §6.5.6 Multiplicative operators, paragraphs 6 and 7 and footnote on truncation toward zero. Checked 22 November 2026.
- ECMA-262 ECMAScript Language Specification. §6.1.6.1.6 Number::remainder. Checked 22 November 2026.