Modulo Calculator: Find a mod n, Negatives Included
Enter a dividend and a divisor to get a mod n with the working shown. Negative numbers are answered under all three common rules, side by side.
You entered a decimal. The remainder of a decimal division is worked out the same way: subtract the largest whole multiple of n. The table of nearby values is shown for whole numbers only.
| a | Floored | Truncated |
|---|
This tool runs in your browser. Nothing you type is uploaded or saved, and there is nothing to install or sign up for.
What a mod n means
The modulo operation gives the remainder left after one whole number is divided by another. Written a mod n, it asks: after taking out as many full copies of n as possible from a, what is left? For 17 mod 12, one full 12 comes out and 5 is left, so the answer is 5.
That sounds like a small idea, yet it sits behind clocks, calendars, check digits, hash tables and most of modern cryptography. This calculator gives the remainder, shows the line of working that proves it, and deals honestly with the one case that trips people up: negative numbers.
How to get your remainder
- Type the number being divided in Dividend (a). It can be negative.
- Type the number you are dividing by in Divisor / modulus (n). Anything except zero is allowed.
- Read the large number, which uses the floored rule from math class. The rows beneath give the truncated and Euclidean results and tell you whether the rules agree.
- Check the Working box, where each result is written as a = n × q + r so you can verify it by hand.
- Use Copy result to take the answer with you, or Reset to return to 17 mod 12.
The table of nearby dividends shows the remainders just before and after your number, which makes the repeating cycle easy to see.
Worked example: 17 mod 12 on a clock
A 24-hour time of 17:00 is 5 o’clock on a 12-hour dial, and that conversion is a modulo sum. Divide 17 by 12: it goes once, and 12 × 1 = 12. Subtract to find what is left: 17 − 12 = 5. The calculator shows this as 17 = 12 x 1 + 5, and since both numbers are positive, all three rules give 5.
Clock arithmetic is the classic picture of modulo. The numbers climb to the modulus and then wrap around to the start, which is why adding hours never produces a time such as 25 o’clock.
Negative numbers: three rules, three names
When a and n are both positive, there is only one sensible remainder. When one of them is negative, mathematicians and programming languages made different choices, and each choice is internally consistent. Take −7 mod 3:
| Rule | Remainder has the sign of | Used by | −7 mod 3 | 7 mod −3 |
|---|---|---|---|---|
| Floored | The divisor | Most math textbooks, Python | 2 | −2 |
| Truncated | The dividend | The % operator in JavaScript, C and Java | −1 | 1 |
| Euclidean | Never negative | Number theory | 2 | 1 |
The difference comes from how the quotient is rounded. Dividing −7 by 3 gives about −2.33. The floored rule rounds the quotient down to −3, and −7 − (3 × −3) leaves 2. The truncated rule chops the quotient toward zero to get −2, and −7 − (3 × −2) leaves −1. The Euclidean rule picks whichever quotient makes the remainder zero or positive.
When the two main rules disagree, their answers always differ by exactly the size of n. That gives programmers a simple fix. If your language returns a negative remainder and you wanted a positive one, add n once.
The formula behind each result
Every answer satisfies the same equation, with a different choice of whole-number quotient q:
a = n x q + r
- Floored:
q = floor(a / n), the quotient rounded down. - Truncated:
q = trunc(a / n), the quotient with its decimal part cut off. - Euclidean: q is chosen so that r is at least 0 and less than the absolute value of n.
Decimals are accepted as well. For 5.5 mod 2, two whole 2s come out and 1.5 remains. To see the quotient digits appear in turn, the long division calculator writes out the full working for whole numbers.
Where modulo turns up in real life
Even or odd. A whole number is even when it leaves 0 after division by 2 and odd when it leaves 1. For example 37 mod 2 = 1, so 37 is odd. Any divisibility test works the same way: a result of 0 means n divides a exactly, and the prime factorization calculator will list every prime that does.
Days of the week. Weekdays repeat every 7 days. To find the weekday 100 days from now, work out 100 mod 7 = 2 and count two days forward. A normal year has 365 days, and 365 mod 7 = 1, which is why a date usually falls one weekday later the following year. For actual calendar dates, the add days to date calculator does the counting for you.
Cycling through a list. Programmers use i mod n to loop around an array, to deal players into teams in turn, or to stripe every second table row.
The modular power box
The lower section computes base^exponent mod n without ever forming the gigantic power. It uses square-and-multiply: square the base repeatedly, reduce mod n after each step, and multiply in the squares that match the binary digits of the exponent. The starting example, 413 mod 497, comes out as 445. This operation is the workhorse of public-key cryptography, though the box here is for learning and checking exercises and takes numbers of up to 15 digits. If you want the huge power itself, the big number calculator prints every digit.
Mistakes to avoid
- Dividing by zero. a mod 0 has no value, and the calculator says so instead of printing a number.
- Assuming your language matches your textbook. In Python,
-7 % 3is 2. In JavaScript, C and Java it is −1. Always check which rule applies before porting a formula. - Confusing the remainder with the decimal part. 17 ÷ 12 is about 1.4167, yet the remainder is 5, not 4167. The remainder is the decimal part multiplied back by n.
- Mixing up the order. 17 mod 12 is 5, while 12 mod 17 is 12, because 17 does not fit into 12 at all.
Students meeting remainders for the first time can pair this page with the LCM and GCF calculator, since the fastest way to find a greatest common factor is repeated modulo. Further math calculators and the whole library of free online tools are open to use without an account.
Frequently asked questions
What is 17 mod 12?
It is 5. Twelve goes into 17 once, and 17 minus 12 leaves 5, which is why 17:00 on a 24-hour clock is 5 o’clock.
How do you calculate a mod n by hand?
Divide a by n and keep only the whole-number part of the quotient. Multiply that by n and subtract the result from a. For 100 mod 7, 7 times 14 is 98 and 100 minus 98 is 2.
What is -7 mod 3?
It depends on the rule. With the floored rule used in math and Python the answer is 2. With the truncated rule used by the % operator in JavaScript, C and Java it is -1.
Why does Python give a different remainder from JavaScript?
Python rounds the quotient down, so the remainder takes the sign of the divisor. JavaScript cuts the quotient toward zero, so the remainder takes the sign of the dividend. The two results differ only when the signs of the numbers differ.
Can you take the mod of a decimal?
Yes. Remove as many whole copies of n as fit and keep what is left. For 5.5 mod 2 the answer is 1.5.
What is modular exponentiation?
It is finding the remainder of a power, such as 4 to the 13th mod 497, which is 445. Square-and-multiply gets the answer by reducing after each step, so the full power never has to be written out.
