Modulo Calculator
Calculate a mod b with quotient and remainder. Step-by-step explanation with clock arithmetic visual and negative number handling.
About Modulo Calculator
Calculate a mod b and see the quotient, remainder, and step-by-step working. Handles negative numbers correctly using mathematical modulo (always non-negative) and shows how this differs from the % operator in most programming languages.
What Is the Modulo Operation?
Modulo gives the remainder after integer division. The relationship is:
a = q × b + r where 0 ≤ r < b
Here q is the quotient (floor of a/b) and r is the remainder (a mod b).
Worked example: 17 mod 5
- Divide: 17 ÷ 5 = 3.4
- Floor quotient: q = 3
- Remainder: r = 17 - (3 × 5) = 17 - 15 = 2
- Result: 17 mod 5 = 2
Modulo Quick Reference
| a | b | a mod b | Quotient | Working |
|---|---|---|---|---|
| 23 | 7 | 2 | 3 | 23 - 3×7 = 23 - 21 = 2 |
| 100 | 12 | 4 | 8 | 100 - 8×12 = 100 - 96 = 4 |
| 50 | 8 | 2 | 6 | 50 - 6×8 = 50 - 48 = 2 |
| 15 | 5 | 0 | 3 | Evenly divisible |
| 7 | 10 | 7 | 0 | 7 < 10, so remainder is 7 |
| 365 | 7 | 1 | 52 | 365 days = 52 weeks + 1 day |
How Does Modulo Handle Negative Numbers?
This is where programming languages disagree. There are two conventions:
| Convention | -7 mod 3 | Used By |
|---|---|---|
| Mathematical (Euclidean) | 2 | Mathematics, this calculator |
| Truncated (% operator) | -1 | JavaScript, C, C++, Java |
| Floored | 2 | Python, Ruby |
Why the difference? It depends on how "quotient" is defined. Truncated division rounds toward zero (so -7/3 = -2), giving remainder -1. Floored division rounds down (so -7/3 = -3), giving remainder 2. Mathematical modulo guarantees the result is always between 0 and b-1.
Converting between them: If the % operator gives a negative result, add b to get the mathematical modulo: -1 + 3 = 2.
Clock Arithmetic - The Most Intuitive Example
A 12-hour clock is modulo 12 in action. Hours "wrap around" after 12:
| 24-Hour Time | mod 12 | 12-Hour Clock |
|---|---|---|
| 0 (midnight) | 0 → 12 | 12:00 AM |
| 9 | 9 | 9:00 AM |
| 13 | 1 | 1:00 PM |
| 18 | 6 | 6:00 PM |
| 23 | 11 | 11:00 PM |
| 25 (next day) | 1 | 1:00 AM |
Days of the week work the same way with mod 7. If today is Wednesday (day 3) and you count forward 100 days: (3 + 100) mod 7 = 103 mod 7 = 5, which is Friday.
Common Uses of Modulo in Programming
| Use Case | Pattern | Example |
|---|---|---|
| Check even/odd | n % 2 | 7 % 2 = 1 (odd), 8 % 2 = 0 (even) |
| Wrap array index | i % length | Index 7 in array of length 5 → position 2 |
| Cycle through items | counter % n | Alternate 3 colours: counter % 3 |
| Extract last digit | n % 10 | 1234 % 10 = 4 |
| Check divisibility | n % d == 0 | Is 45 divisible by 9? 45 % 9 = 0, yes |
| Hash table sizing | hash % buckets | Map hash to bucket index |
| Format every nth item | i % n == 0 | Add line break every 5 items |
Divisibility Rules Using Modulo
A number is divisible by d if and only if n mod d = 0:
| Test | Check | Example |
|---|---|---|
| Divisible by 2? | n mod 2 = 0 | 146 mod 2 = 0 → yes |
| Divisible by 3? | n mod 3 = 0 | 246 mod 3 = 0 → yes (digit sum = 12) |
| Divisible by 5? | n mod 5 = 0 | 345 mod 5 = 0 → yes |
| Divisible by 7? | n mod 7 = 0 | 343 mod 7 = 0 → yes (7³) |
Modular Arithmetic in Cryptography
Modular arithmetic is fundamental to modern cryptography. RSA encryption relies on modular exponentiation: computing a^b mod n for very large numbers. Diffie-Hellman key exchange, digital signatures, and hash functions all use modular arithmetic extensively. The security comes from the fact that while computing a^b mod n is fast, reversing the operation (the discrete logarithm problem) is computationally infeasible for large numbers.
For full step-by-step division working, the long division calculator shows every bring-down step. For finding common factors and multiples, the GCF and LCM calculator uses prime factorisation.
All calculations run in your browser. No data is sent to any server.
Frequently Asked Questions
What is modulo?
Modulo (mod) gives the remainder after division. For example, 17 mod 5 = 2 because 17 divided by 5 is 3 with remainder 2.
How does modulo work with negative numbers?
The mathematical modulo always returns a non-negative result. So -7 mod 3 = 2 (not -1). This differs from JavaScript's % operator, which preserves the sign of the dividend.
What is clock arithmetic?
Clock arithmetic is a real-world example of modulo 12. After 12, the clock wraps around. So 15 o'clock on a 12-hour clock is 3 o'clock (15 mod 12 = 3).
How do I check if a number is divisible?
If a mod b equals 0, then b divides a evenly with no remainder. The calculator shows a 'Evenly divisible?' indicator for this.
Where is modulo used in programming?
Modulo is used for checking even/odd numbers, wrapping array indices, hash table indexing, formatting (every nth item), and time calculations.
Related Tools
Link to this tool
Copy this HTML to link to this tool from your website or blog.
<a href="https://toolboxkit.io/tools/modulo-calculator/" title="Modulo Calculator - Free Online Tool">Try Modulo Calculator on ToolboxKit.io</a>