What the modulo operation does
The modulo operation gives the remainder left over after whole-number division. Dividing a by n produces a quotient q and a remainder r that satisfy
a = n × q + r
For positive numbers there is no ambiguity: 17 ÷ 5 = 3 remainder 2, so 17 mod 5 = 2. Modulo is everywhere in daily life — the time 15 hours after 10 o'clock is (10 + 15) mod 12 = 1 o'clock, and the weekday 100 days after a Monday is found from 100 mod 7 = 2, so it is a Wednesday.
Programmers use it to test divisibility (n % 2 == 0 for even numbers), to wrap array indices around, and in hashing; mathematicians build whole number systems on it ("arithmetic modulo n"). Two numbers are congruent modulo n, written a ≡ b (mod n), when they leave the same remainder.
Negative numbers: three conventions
When a or n is negative, the quotient can be rounded in different directions, and each choice gives a different remainder. For the default example −17 mod 5:
| Convention | Quotient rule | q | r | Used by |
|---|---|---|---|---|
| Truncated | round toward zero | −3 | −2 | C, C++, Java, JavaScript, C#, Go, Rust (%) |
| Floored | round down | −4 | 3 | Python %, Ruby, Excel MOD, Lua |
| Euclidean | remainder always ≥ 0 | −4 | 3 | number theory |
All three satisfy a = n·q + r: 5 × (−3) + (−2) = −17 and 5 × (−4) + 3 = −17. With the truncated rule the remainder takes the sign of the dividend; with the floored rule it takes the sign of the divisor; the Euclidean remainder is never negative.
This is a classic source of bugs. Code like index % length in JavaScript returns −2 for index −17 and length 5, which is not a valid array index. The usual fix is ((a % n) + n) % n, which gives the floored/Euclidean result for positive n.
Big numbers and modular exponentiation
This calculator uses arbitrary-precision integers, so the dividend can have hundreds of digits and the remainder is still exact. Decimal inputs are also accepted (5.5 mod 2 = 1.5), but whole numbers are where modulo is most useful.
The second mode computes a^e mod n — the core operation of RSA encryption, Diffie–Hellman key exchange and primality tests. Computing a^e first is hopeless for large e (21000 already has 302 digits), so the calculator uses square-and-multiply: write e in binary, repeatedly square a modulo n, and multiply the squares that correspond to 1-bits into the running result. Every intermediate value stays below n, and the number of steps is only about the number of bits in e.
For example 413 mod 497: 13 = 1101 in binary, the squares of 4 modulo 497 are 4, 16, 256, 429, and multiplying those for the 1-bits gives 4 × 256 × 429 ≡ 445 (mod 497).
Frequently asked questions
What is −1 mod 3?
In mathematics and Python it is 2, because −1 = 3 × (−1) + 2. In C, Java and JavaScript, −1 % 3 gives −1, because those languages round the quotient toward zero.
Is modulo the same as remainder?
For positive numbers, yes. Some languages distinguish them: in Java, % is the truncated remainder while Math.floorMod gives the floored modulo; in Python, % is the floored modulo and math.fmod is the truncated remainder.
What happens if the divisor is 0?
a mod 0 is undefined, just like division by zero, so the calculator asks for a non-zero divisor.
How do I check if a number is divisible by another?
a is divisible by n exactly when a mod n = 0, in every convention. For example 1,001 mod 7 = 0, so 1,001 is a multiple of 7 (7 × 143).
Last reviewed: