What this calculator does
The modulo operation gives the remainder after division. It sounds trivial, but it underpins a surprising amount: cyclical arithmetic, hash tables, checksums, cryptography, and every calculation that wraps around a fixed range like clock time or days of the week.
Negative numbers are where implementations disagree. The mathematical convention keeps the result non-negative, so −7 mod 5 is 3. Most programming languages, including JavaScript, keep the sign of the dividend and return −2 instead. This calculator shows both, because which one you want depends entirely on what you are doing.
The formula
The truncated remainder is what most languages compute, taking the sign of the dividend. The mathematical modulo subtracts the floor of the quotient rather than its truncation, which is what makes the result non-negative for positive divisors. The two agree whenever the dividend is positive.
| Term | Meaning |
|---|---|
| Dividend | The number being divided. |
| Divisor | The number you are dividing by. |
| Remainder | What is left over. |
| Floor quotient | The quotient rounded toward negative infinity. |
| Truncated quotient | The quotient rounded toward zero. |
The inputs explained
| Field | What to enter |
|---|---|
| Dividend a | The dividend. Negative values are handled, and the two remainder conventions will differ. |
| Divisor n | The divisor. Cannot be zero. |
When to use it
Wrapping around a cycle
Working out the day of the week 100 days from now is 100 mod 7. Anything that repeats on a fixed cycle reduces to modulo arithmetic.
Testing divisibility
A remainder of zero means the division is exact. This is how even and odd are tested, and how the prime factorisation algorithm decides whether a factor divides.
Distributing items into buckets
Hash tables, load balancers and round-robin schedulers all use modulo to map an arbitrary index onto a fixed number of slots.
Handling negative indices
Wrapping a negative position back into a valid range needs the mathematical modulo, not the truncated one. This is a frequent source of bugs when moving code between languages.
Worked examples
Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.
Dividing by 5
A range of dividends, including negatives where the two conventions diverge.
| Dividend | Remainder (a mod n) | Truncated remainder | Integer quotient | Floor quotient |
|---|---|---|---|---|
| -7 | 3.000 | -2.000 | -1 | -2 |
| -1 | 4.000 | -1.000 | -0 | -1 |
| 0 | 0 | 0 | 0 | 0 |
| 7 | 2.000 | 2.000 | 1 | 1 |
| 12 | 2.000 | 2.000 | 2 | 2 |
| 25 | 0 | 0 | 5 | 5 |
The same dividend across divisors
Seventeen divided by a range of divisors.
| Divisor | Remainder (a mod n) | Integer quotient | Divides evenly? |
|---|---|---|---|
| 2 | 1.000 | 8 | No |
| 3 | 2.000 | 5 | No |
| 5 | 2.000 | 3 | No |
| 8 | 1.000 | 2 | No |
| 17 | 0 | 1 | Yes |
| 20 | 17.000 | 0 | No |
Questions
What is the difference between mod and remainder?
They agree for positive numbers. For negatives, the mathematical modulo returns a non-negative result while the remainder keeps the sign of the dividend. −7 mod 5 is 3 mathematically but −2 in most programming languages.
How do I get a non-negative result in code?
Use ((a % n) + n) % n. Adding the divisor and taking the modulo again shifts any negative result back into the correct range.
What is modulo used for?
Cyclical arithmetic, hash functions, checksums such as the Luhn algorithm on card numbers, random number generation, and most of public-key cryptography, which is built on modular exponentiation.
Can the divisor be negative?
Mathematically yes, though the conventions become murkier. Most practical uses involve a positive divisor, and that is where the two definitions have their clearest meaning.
What happens if I divide by zero?
It is undefined. The calculator cannot return a meaningful result, and every programming language will either throw an error or produce a special not-a-number value.
For factors and divisibility, see prime factorisation or GCD and LCM.