Modulo Calculator
The modulo operation returns the remainder of integer division. 17 modulo 5 is 2, because 17 divided by 5 gives 3 with a remainder of 2.
It answers "how much is left over" rather than "how many times does it fit".
When the remainder is zero, the first number is divisible by the second — and that is how divisibility is tested in virtually all code.
This is where languages diverge, and it is the most common source of modulo bugs.
In JavaScript, Java, C and C# the result takes the sign of the dividend: minus 7 modulo 3 gives minus 1. In Python and Ruby the result takes the sign of the divisor: the same calculation gives 2.
Both conventions are mathematically defensible, but they produce different values. If you port code between languages and negatives are involved, this is the first place to check.
The strict mathematical definition of modulo calls for a non-negative result — the Python version. The version following the dividend’s sign is, strictly, the remainder of truncated division.
- Testing whether a number is even: modulo 2 equals zero.
- Alternating row colours in a table: the index modulo 2 picks between two classes.
- Distributing items cyclically across N destinations, as in simple load balancing.
- Clock arithmetic: adding 8 hours to 20:00 gives 04:00, which is 28 modulo 24.
- Check digits on identity numbers and barcodes, almost all based on modulo 11 or modulo 10.
Frequently asked questions
Because languages use different conventions. JavaScript, Java and C follow the dividend’s sign; Python and Ruby follow the divisor’s. Minus 7 modulo 3 gives minus 1 in one and 2 in the other.
For positive numbers, yes. With negatives, "remainder" usually refers to truncated division and "modulo" to the mathematical non-negative definition. The difference only shows up there.
Check whether the modulo is zero. A zero remainder means exact division.
The operation is undefined, just like division by zero. The tool rejects the input rather than returning a meaningless value.