All tools › Math Calculators › Arithmetic
Find a mod n with the quotient, and compare the floored and truncated results for negative numbers.
Find the remainder of a division: a mod n
The modulo operation gives the remainder left over when one whole number is divided by another. 17 mod 5 is 2, because 5 goes into 17 three times with 2 left. This modulo calculator shows that remainder, the quotient that goes with it.
It also handles the case that causes real confusion: negative numbers. There are two common ways to define the remainder when a sign is negative, and programming languages do not agree on which to use. The calculator shows both side by side, so you can see which one your textbook or your code is using.
How to use the modulo calculator
- Type the number being divided into Dividend (a). It can be negative.
- Type the number you divide by into Divisor / modulus (n). It can be negative but not zero.
- Press the button. The large result is the floored remainder, the one used in mathematics.
- Compare the rows Floored remainder (math, Python) and Truncated remainder (JavaScript, C, Java). When both numbers are positive they are the same.
The formula and the two conventions
a = n x q + r, so r = a – n x q
Here a is the dividend, n the divisor, q a whole-number quotient and r the remainder. Every convention satisfies this equation. They differ only in how the quotient q is rounded when a / n is not a whole number.
- Floored: q is rounded down, toward negative infinity. The remainder then has the same sign as the divisor n. This is the usual meaning of “mod” in math, and it is what the % operator returns in Python.
- Truncated: q is rounded toward zero, by dropping the decimal part. The remainder then has the same sign as the dividend a. This is what the % operator returns in JavaScript, C and Java.
- Euclidean: the remainder is never negative, whatever the signs. For a positive divisor it equals the floored result.
Worked example: -17 mod 5
Enter -17 as the dividend and 5 as the divisor. The exact quotient is -3.4.
| Convention | Quotient | Remainder | Quotient and check row |
|---|---|---|---|
| Floored (math, Python) | -4 | 3 | -4, because 5 x (-4) + 3 = -17 |
| Truncated (JavaScript, C, Java) | -3 | -2 | -3, because 5 x (-3) + (-2) = -17 |
| Euclidean | -4 | 3 | never negative |
Rounding -3.4 down gives -4, and -17 – (5 x -4) = 3. Rounding -3.4 toward zero gives -3, and -17 – (5 x -3) = -2. Both are valid remainders; they differ by exactly 5, the divisor, and the row “Do the two conventions agree?” says so.
Reading the other rows
- Divides evenly? says yes when the remainder is 0, meaning a is a multiple of n.
- Numbers with the same floored remainder lists neighbors of a that are n apart, such as -27, -22, -17, -12, -7. They are all congruent modulo 5.
- Nearby dividends is a table of the five numbers below and above a.
Where modulo is used
- Clocks. Five hours after 10 o’clock is (10 + 5) mod 12 = 3 o’clock. In the same way 29 mod 12 = 5.
- Even or odd. A number is even when it is 0 mod 2.
- Days of the week. 100 days from now is 100 mod 7 = 2 weekdays later.
- Wrapping around a list in code, where an index must return to the start after the last item.
A common bug with negative numbers
In a language that truncates, a line such as index = (i – 1) % length returns -1 when i is 0, which is not a valid position. The usual fix is ((x % n) + n) % n, which always lands between 0 and n – 1 for a positive n.
Limits and assumptions
- Whole numbers only, each between minus one trillion and one trillion.
- The divisor cannot be zero.
- Decimal operands, such as 7.5 mod 2, are not supported.
Frequently asked questions
What is the difference between modulo and remainder?
For positive numbers, nothing. With a negative number, “remainder” often means the truncated result, which keeps the sign of the dividend, while “modulo” usually means the floored result, which keeps the sign of the divisor.
What is a mod n when a is smaller than n?
For positive numbers it is a itself. 3 mod 7 is 3, because 7 goes into 3 zero times and all of 3 is left over.
Why is mod 0 not allowed?
Finding a remainder requires dividing by n, and division by zero is undefined. Most programming languages report an error for it.
What does “congruent modulo n” mean?
Two numbers are congruent modulo n when they leave the same remainder after division by n, which is the same as saying their difference is a multiple of n. 17 and 2 are congruent modulo 5.
Related calculators
- Long Division Calculator – quotient and remainder with the working shown.
- GCF Calculator – Euclid’s method for the greatest common factor is built on remainders.
- Prime Number Checker – test whether a number has any divisor that leaves remainder 0.
- Day of the Week Calculator – weekday arithmetic, which is counting mod 7.
