Modulo explained: remainders, clocks and negative numbers

Modulo gives the remainder after division. 17 mod 5 is 2, because 17 = 3 × 5 + 2. It is the arithmetic of anything that wraps around, from clock faces to days of the week. The one tricky part is negative numbers, where programming languages disagree. The modulo calculator shows every convention side by side, and this guide explains why they differ.

What modulo means

Dividing a by n gives a whole-number quotient q and a remainder r, with a = q × n + r. For positive numbers, r is between 0 and n − 1, and a mod n is that r. So 20 mod 6 = 2, 18 mod 6 = 0, and 4 mod 6 = 4, because 6 goes into 4 zero times. A remainder of 0 means n divides a exactly, which is why n mod 2 is the standard test for even and odd numbers.

Clock and calendar arithmetic

Clocks are the classic example. Five hours after 9 o'clock is 14, and 14 mod 12 = 2, so 2 o'clock. A 24-hour clock wraps at 24 instead, which is the idea behind Military time, explained in one rule. Days of the week wrap at 7: 100 days after a Monday, 100 mod 7 = 2, so it is a Wednesday. The same trick splits a total into units: 1234 seconds is 1234 ÷ 60 = 20 minutes with 1234 mod 60 = 34 seconds left over.

Negative numbers

What is −7 mod 3? There are two reasonable ways to divide −7 by 3:

  • Truncate the quotient towards zero: −7 ÷ 3 = −2.33 becomes −2, and −7 = −2 × 3 − 1, so the remainder is −1.
  • Floor the quotient downwards: −2.33 becomes −3, and −7 = −3 × 3 + 2, so the remainder is 2.

Both satisfy a = q × n + r; they just choose a different q. Mathematicians usually want the second kind of answer, a remainder between 0 and n − 1 that is never negative, called the Euclidean remainder. On a clock, 7 hours before 0 o'clock on a 3-hour dial is position 2, not −1.

Modulo in programming

The % operator follows different conventions in different languages:

  • JavaScript, C, C++, Java and C# truncate, so the result takes the sign of the dividend: −7 % 3 is −1, and 7 % −3 is 1.
  • Python floors, so the result takes the sign of the divisor: −7 % 3 is 2, and 7 % −3 is −2.
  • Excel's MOD function also returns a result with the sign of the divisor: MOD(−7, 3) is 2.

This is a common source of bugs, for example when stepping backwards through an array or a carousel and getting a negative index. In JavaScript the usual fix is ((a % n) + n) % n, which always returns a value from 0 to n − 1 when n is positive. Python's math.fmod gives the truncated version if you need to match C. The calculator shows the Euclidean, floored and truncated results for any pair of whole numbers, plus the quotient, exactly, even for numbers far beyond what a calculator can hold.

Modular arithmetic

Working modulo n means treating numbers as equal when they leave the same remainder. We write 17 ≡ 2 (mod 5). The useful property is that you can reduce at any stage of addition or multiplication without changing the final remainder. That makes huge calculations small. The last digit of 7²⁰²⁶ is 7²⁰²⁶ mod 10. Powers of 7 end in 7, 9, 3, 1 and then repeat every four, and 2026 mod 4 = 2, so the last digit is the second in the cycle: 9.

Check digits use the same idea. ISBN-13 and many barcodes use a weighted sum mod 10, and ISBN-10 uses mod 11, so a single mistyped digit changes the remainder and is caught; the check digit calculator shows the working.

Powers, inverses and encryption

The calculator also does modular exponentiation, bᵉ mod m, using repeated squaring so that even enormous exponents take only a few steps. Its default example, 3²⁰⁰ mod 13, is 9: since 3³ = 27 ≡ 1 (mod 13), the powers cycle every three, and 200 leaves remainder 2, so the answer is 3² = 9. It also finds modular inverses, the number that multiplies with a to give 1. The inverse of 3 mod 11 is 4, because 3 × 4 = 12 ≡ 1. An inverse exists only when a and m share no common factor, which the GCD calculator checks. These two operations, on numbers hundreds of digits long built from large primes, are the core of RSA and Diffie–Hellman; Prime numbers explained: how to test, list and use them explains the prime side.

Sources and further reading

The claims in this guide rest on these references, which were checked when the guide was last updated. Spotted an error? The contact page says how to report it.

  1. Modulo — Wikipedia (conventions by language)
  2. Binary arithmetic operations — Python language reference
  3. MOD function — Microsoft Support (result takes the sign of the divisor)

Try the tool

Frequently asked questions

What does mod mean in maths?

a mod n is the remainder when a is divided by n. For example, 17 mod 5 is 2.

Why is −7 % 3 different in Python and JavaScript?

JavaScript truncates the quotient towards zero, giving −1, while Python floors it, giving 2. Both satisfy a = q × n + r.

How do I get a positive modulo in JavaScript?

Use ((a % n) + n) % n, which returns a value from 0 to n − 1 when n is positive.

What is modulo used for?

Wrap-around counting such as clocks and weekdays, even and odd tests, check digits, hashing and encryption.