Skip to content
K Knidox Search…
Math · Number Theory

Modulo Calculator

Find a mod n — the remainder of a ÷ n — with the quotient and a worked breakdown.

The number being divided. May be negative.
The modulus. Must be a non-zero integer.
17 mod 5
2

Quotient ⌊17 ÷ 5⌋ = 3, remainder = 2. Uses the mathematical convention, so the remainder is always 0 ≤ r < 5 (non-negative even when a is negative — unlike JavaScript’s % operator).

The modulo a mod n is the remainder left after dividing a by n. To find it, take the largest whole-number multiple of n that fits inside a and subtract it. For 17 mod 5, the largest multiple of 5 not exceeding 17 is 15 (= 3 × 5), so 17 mod 5 = 17 − 15 = 2, because 17 = 3 × 5 + 2.

What modulo means

When you divide an integer a (the dividend) by an integer n (the divisor or modulus), you get a whole-number quotient q and a leftover remainder r. The modulo operation, written a mod n, returns that remainder. It satisfies a = q × n + r, where q is the floor of a ÷ n and r sits in the range 0 ≤ r < n. Because the remainder can never reach n, the result of a mod n always lands somewhere between 0 and n − 1.

a mod n = a − n × ⌊a ÷ n⌋

⌊x⌋ is the floor — the largest integer not greater than x; n must be non-zero

Worked example

Compute 17 mod 5:

  1. 1
    Divide a by n. 17 ÷ 5 = 3.4, so the floor quotient is ⌊3.4⌋ = 3.
  2. 2
    Multiply the quotient back by n. 3 × 5 = 15 — the largest multiple of 5 that fits inside 17.
  3. 3
    Subtract to get the remainder. 17 − 15 = 2, so the remainder r = 2.
  4. 4
    Confirm the division statement. 17 = 3 × 5 + 2, so 17 mod 5 = 2.

Modulo examples

Each row uses the mathematical convention, where the remainder is always 0 ≤ r < n.

Expressiona = q × n + rResultUse
17 mod 517 = 3 × 5 + 22Plain remainder
−1 mod 5−1 = (−1) × 5 + 44Negative dividend (≠ JS −1)
8 mod 28 = 4 × 2 + 00Even number
7 mod 27 = 3 × 2 + 11Odd number
15 mod 1215 = 1 × 12 + 33Clock arithmetic (3 o’clock)
25 mod 1225 = 2 × 12 + 11Clock arithmetic (1 o’clock)

Where modulo is used — and the negative-number catch

Even and odd. A number is even exactly when n mod 2 = 0 and odd when n mod 2 = 1. This parity check is the most common everyday use of modulo.

Clocks and cyclic counting. Modulo wraps numbers into a repeating cycle. A 12-hour clock is arithmetic mod 12: 15 mod 12 = 3, so 15:00 reads as 3 o’clock. The same idea covers days of the week (mod 7), angles (mod 360°), and any periodic schedule.

Hashing and indexing. Hash tables and round-robin schemes map a large key down to a fixed number of buckets with key mod size, guaranteeing an index inside the valid range.

The negative-number convention. This calculator uses the mathematical convention, where the remainder is always non-negative: −1 mod 5 = 4, computed via ((a % n) + n) % n. Many programming languages disagree. JavaScript’s % operator keeps the sign of the dividend, so -1 % 5 evaluates to -1, not 4 — a frequent source of bugs when wrapping indices. If you need a guaranteed non-negative result in code, wrap the language’s % with ((a % n) + n) % n.

What is the modulo operation?
Modulo, written a mod n, returns the remainder after dividing a by n. It satisfies a = q × n + r, where q is the floor quotient and r is the remainder. For example 17 mod 5 = 2, because 17 = 3 × 5 + 2.
How does modulo handle negative numbers?
This calculator uses the mathematical convention, where the remainder is always non-negative: 0 ≤ r < n. So −1 mod 5 = 4, since −1 = (−1) × 5 + 4. It is computed as ((a % n) + n) % n.
How do I use modulo to check if a number is even or odd?
Take the number mod 2. If n mod 2 = 0 the number is even; if n mod 2 = 1 it is odd. For instance 8 mod 2 = 0 (even) and 7 mod 2 = 1 (odd).
What is clock arithmetic and how does modulo relate to it?
A 12-hour clock is arithmetic mod 12: hours wrap around after reaching 12. So 15 mod 12 = 3, meaning 15:00 shows as 3 o’clock, and 25 mod 12 = 1. The same wrapping handles weekdays (mod 7) and angles (mod 360).
Why does my code give a different answer for negative numbers?
Many languages, including JavaScript, define % to keep the sign of the dividend, so -1 % 5 is -1, not 4. That is the remainder operator, which differs from the mathematical modulo. To force a non-negative result, use ((a % n) + n) % n.
What is the difference between the remainder and the modulo?
For positive numbers they are identical. They diverge only when the dividend is negative: the mathematical modulo is always non-negative (−1 mod 5 = 4), while a remainder operator like JavaScript’s % can return a negative value (−1 % 5 = −1).