Modulo Formula
Equivalently: remainder when a is divided by m
Example: 17 mod 5 = 2 (since 17 = 5×3 + 2)
Modulo Examples
| Expression | Result | Explanation |
|---|---|---|
| 10 mod 3 | 1 | 10 = 3×3 + 1 |
| 15 mod 5 | 0 | 15 = 5×3 + 0 (exact) |
| 7 mod 2 | 1 | 7 = 2×3 + 1 (odd check) |
| 100 mod 7 | 2 | 100 = 7×14 + 2 |
| −7 mod 3 | 2 | Math mod (always non-negative) |
| 17.5 mod 5 | 2.5 | 17.5 = 5×3 + 2.5 |
Uses of Modulo
- Even/Odd testing: n mod 2 = 0 → even; n mod 2 = 1 → odd
- Clock arithmetic: (current_hour + N) mod 12 for time calculations
- Cyclic patterns: Days of the week, calendar repetition
- Cryptography: RSA encryption relies heavily on modular arithmetic
- Hash tables: Index = hash(key) mod table_size
- Circular buffers: Position = (position + 1) mod buffer_size
- Divisibility testing: n mod d = 0 means n is divisible by d
Frequently Asked Questions
What is the difference between mod and remainder in programming?
In mathematics, modulo always returns a non-negative result. In programming, behavior with negative numbers varies: Python's % always returns non-negative (matching math). C, Java, and JavaScript's % return the sign of the dividend. For example, −7 % 3 = −1 in JavaScript but 2 in Python. This calculator uses the mathematical (always non-negative) convention.
What does it mean when the modulo is 0?
If a mod m = 0, then a is exactly divisible by m with no remainder. This is how you test divisibility: 12 mod 4 = 0 means 4 divides 12 evenly. This is used in prime testing, finding common factors, and many algorithms.
Can I use modulo with decimals?
Yes — modulo works with real numbers, not just integers. 7.5 mod 2.5 = 0 (exact), 10.7 mod 3 = 1.7. This is useful in graphics (wrapping coordinates), audio (phase wrapping), and scientific computing.