Modulo Calculator
Imagine you are writing a load-balancer that assigns each incoming request to one of five servers. Request 17 lands on server 2, request 20 on server 0, and request 23 on server 3. The pattern is not random–it is produced by the modulo operation, which returns the remainder after dividing one integer by another. This wrap-around logic appears everywhere from clock faces to cryptographic algorithms.
The modulo calculator above returns the remainder and quotient for any pair of integers you provide, handling both positive and negative values.
What Does a Modulo Calculator Do?
In mathematics, the expression a mod n asks: what remains after a is split into as many whole n pieces as possible? The number a is the dividend, n is the divisor (also called the modulus), and the output is the remainder.
For example, 29 mod 6 = 5 because 6 fits into 29 four whole times (4 × 6 = 24) and 29 − 24 = 5. If the dividend is smaller than the divisor, the result is simply the dividend itself: 4 mod 9 = 4.
The standard formula is:
a mod n = a − n × ⌊a / n⌋
where ⌊x⌋ denotes the floor function–rounding down to the nearest integer.
How Do You Calculate Modulo by Hand?
You can compute a mod n in four steps:
- Divide a by n.
- Round the quotient down to the nearest integer (take the floor).
- Multiply n by that integer.
- Subtract the product from the original a.
Example: calculate 31 mod 7.
- 31 / 7 = 4.428…
- Floor = 4
- 4 × 7 = 28
- 31 − 28 = 3
So 31 mod 7 = 3.
If you are following a programming language that truncates toward zero rather than flooring, replace step 2 with truncation. That single change explains why calculators and compilers sometimes disagree on negative numbers.
Modulo with Negative Numbers
Negative operands break the simple “remainder” intuition because the result depends on how you round the intermediate quotient.
Under floor division (mathematics, Python, Ruby):
- −10 / 3 floors to −4.
- −10 − (−4 × 3) = 2.
- Therefore, −10 mod 3 = 2.
Under truncated division (C, Java, JavaScript, Go):
- −10 / 3 truncates to −3.
- −10 − (−3 × 3) = −1.
- Therefore, −10 % 3 = −1.
Both values satisfy a = q × n + r, but only floor division guarantees that r is non-negative whenever n is positive.
| System | Rounding Rule | −10 mod 3 |
|---|---|---|
| Python, Ruby, Mathematics | Floor (−∞) | 2 |
| C, Java, JavaScript, Go | Truncate (toward 0) | −1 |
Programming Differences
Most languages use the percent symbol (%) for modulo, yet its behavior is not uniform. Python’s % is a true modulo operator based on floor division. C and Java treat % as a remainder operator based on truncation. When you port algorithms across languages, the edge case of negative dividends is the most common source of bugs.
Real-World Uses of Modulo
- Circular indexing:
array[i % length]wraps back to the start of an array when i exceeds the boundary. - Parity checks:
n % 2equals 0 for even numbers and 1 for odd numbers. - Clock arithmetic: 22:00 plus 5 hours is
(22 + 5) mod 24 = 3, giving 03:00. - Cryptography: RSA and Diffie-Hellman rely on modular exponentiation
a^b mod nto keep numbers manageable.
Key Properties of Modular Arithmetic
- The result of
a mod nis always less than|n|when n ≠ 0. - If a is divisible by n, then
a mod n = 0. 0 mod n = 0for any non-zero n.(a × b) mod n = ((a mod n) × (b mod n)) mod n. This identity lets software perform huge multiplications without integer overflow.