Modulo Calculator
Free modulo calculator with step-by-step proof and verification. Calculate a mod n instantly, see quotient and remainder, compare math vs. programming conventions, and explore modular arithmetic patterns. Works with negative numbers.
The number to divide
Divide by this number (cannot be 0)
17 mod 5 = 2
Verification
Proof that the result is correct
a = n × q + r
17 = 5 × 3 + 2
Step-by-Step Solution
How the modulo was calculated
Identify the operation
17 mod 5
Dividend (a) = 17, Divisor (n) = 5
Find the remainder when a is divided by n.
Divide
17 ÷ 5 = 3.4
The exact quotient is 3.4.
Find the integer quotient
q = floor(3.4) = 3
Use floor division (round toward −∞).
q = 3
Calculate the remainder
r = a − n × q = 17 − 5 × 3
r = 17 − 15
r = 2
Verify
n × q + r = 5 × 3 + 2 = 17
17 = 17
17 = 5 × 3 + 2 ✔
Modulo Table
Pattern of x mod 5 for x = 0..20
Top: x, Bottom: x mod 5 — highlighted cells are divisible
What Is Modulo (Mod)?
The remainder after division
The modulo operation (abbreviated mod) finds the remainder when one integer is divided by another. If you divide 17 by 5, you get 3 with a remainder of 2 — so 17 mod 5 = 2.
Formally, for any integers a (dividend) and n (divisor, n ≠ 0), the modulo operation finds the unique integer r such that:
a = n × q + r where 0 ≤ r < |n|
Here q is the integer quotient (from floor division) and r is the remainder. The remainder is always non-negative in the mathematical definition.
How to Calculate Modulo Step by Step
A simple 4-step method with a worked example
Example: Calculate 27 mod 6.
Step 1: Divide
27 ÷ 6 = 4.5
Step 2: Take the floor
q = floor(4.5) = 4 — round down to the nearest integer (toward −∞).
Step 3: Multiply back
n × q = 6 × 4 = 24
Step 4: Subtract to get remainder
r = a − n × q = 27 − 24 = 3
27 mod 6 = 3 Verify: 6 × 4 + 3 = 27 ✔
Math vs. Programming: Negative Modulo
Why different languages give different answers
When the dividend is negative, math and programming give different results. The mathematical definition always returns a non-negative remainder, while most programming languages return a remainder with the same sign as the dividend.
Example: −17 mod 5
| Language | Convention | Result | Quotient |
|---|---|---|---|
| Python | Floor (math) | 3 | −4 |
| Ruby | Floor (math) | 3 | −4 |
| JavaScript | Truncated | −2 | −3 |
| Java | Truncated | −2 | −3 |
| C / C++ | Truncated | −2 | −3 |
| Go | Truncated | −2 | −3 |
The difference comes from how the quotient is rounded: floor rounds toward −∞ (giving a non-negative remainder), while truncation rounds toward zero (preserving the dividend's sign).
Note: When the divisor is also negative (e.g., 17 mod −5), even Python and Ruby give a negative result (−3). This calculator uses the Euclidean definition, where the remainder is always non-negative regardless of the divisor's sign.
Real-World Applications of Modulo
Where modular arithmetic shows up in everyday life
Clock Arithmetic
15:00 in 24-hour time is 15 mod 12 = 3 PM. Clocks “wrap around” every 12 hours — pure modular arithmetic.
Even / Odd Check
n mod 2 = 0 means even, n mod 2 = 1 means odd. This is the most common use of modulo in programming.
Day of the Week
What day is it 100 days from Monday? 100 mod 7 = 2, so it's Wednesday (2 days after Monday).
Checksums (ISBN, IBAN)
ISBN-13 uses a check digit calculated with mod 10. IBAN validation uses mod 97. These catch data entry errors.
Cryptography
RSA encryption relies on modular exponentiation. Diffie-Hellman key exchange uses modular arithmetic for secure communication.
Hash Functions
Hash tables use mod to map keys to bucket indices: index = hash(key) mod tableSize. This distributes data evenly.
Frequently Asked Questions
Common questions about the modulo operation