Modulo Calculator
a mod n is what is left after you take as many whole n's out of a as you can. 71 mod 3 = 2, because 3 × 23 = 69 and 71 − 69 = 2 (see it worked out). Negative numbers and numbers of any length work too.
Calculate a mod n
Whole numbers of any size, positive or negative. The result updates as you type.
Check your input
- Quotient
- 0
- Divides evenly
- No
- Add to reach a multiple
- 0
- Congruence
- 0
On the number line
Remainder Negative remainderMath, Python and JavaScript
| Convention | Used by | Quotient | Remainder |
|---|
How to Use This Calculator
-
Enter a and n
Type the number you are dividing (a) and the number you divide by (n). Minus signs, spaces and very long numbers are all fine.
-
Read the remainder
The answer updates as you type. Press Enter or Calculate to see a message if something is wrong with the input.
-
Check the working
Below the result you get the steps, a number line, and what Python, JavaScript and Excel would return for the same numbers.
How to calculate modulo
Modulo, or mod for short, gives you the remainder of a division. You divide a by n, keep the whole-number part of the answer, and see what is left. If you share 17 sweets among 5 children, each child gets 3 and 2 sweets stay in the bag. In math notation that is 17 mod 5 = 2.
Here is the method for 23 mod 7, one step at a time:
- Divide: 23 ÷ 7 = 3.285…
- Round down to a whole number: 3. This is the quotient.
- Multiply back: 7 × 3 = 21.
- Subtract: 23 − 21 = 2. That is the remainder, so 23 mod 7 = 2.
You can always check the answer with one line: n × quotient + remainder should give you a back. Here 7 × 3 + 2 = 23, so the result is right. For positive numbers the remainder is always at least 0 and smaller than n. Mod 2 can only give 0 or 1, mod 10 gives 0 to 9, and so on. The calculator prints this check line under every result.
Show 23 mod 7 in the calculator
Modulo table: common results
These are the modulo results people look up most often. When a is smaller than n, as in 2 mod 3 or 1 mod 2, n does not fit even once, so the answer is just a. A remainder of 0 means n divides a exactly, as in 10 mod 2.
| Expression | Quotient | Result | Check |
|---|---|---|---|
| 0 mod 2 | 0 | 0 | 2 × 0 + 0 = 0 |
| 0 mod 3 | 0 | 0 | 3 × 0 + 0 = 0 |
| 0 mod 10 | 0 | 0 | 10 × 0 + 0 = 0 |
| 1 mod 2 | 0 | 1 | 2 × 0 + 1 = 1 |
| 1 mod 3 | 0 | 1 | 3 × 0 + 1 = 1 |
| 2 mod 3 | 0 | 2 | 3 × 0 + 2 = 2 |
| 5 mod 2 | 2 | 1 | 2 × 2 + 1 = 5 |
| 5 mod 3 | 1 | 2 | 3 × 1 + 2 = 5 |
| 10 mod 2 | 5 | 0 | 2 × 5 + 0 = 10 |
| 10 mod 3 | 3 | 1 | 3 × 3 + 1 = 10 |
| 10 mod 4 | 2 | 2 | 4 × 2 + 2 = 10 |
| 10 mod 8 | 1 | 2 | 8 × 1 + 2 = 10 |
| 17 mod 5 | 3 | 2 | 5 × 3 + 2 = 17 |
| 23 mod 7 | 3 | 2 | 7 × 3 + 2 = 23 |
| 25 mod 4 | 6 | 1 | 4 × 6 + 1 = 25 |
| 71 mod 3 | 23 | 2 | 3 × 23 + 2 = 71 |
| 100 mod 3 | 33 | 1 | 3 × 33 + 1 = 100 |
| 100 mod 7 | 14 | 2 | 7 × 14 + 2 = 100 |
| 100 mod 8 | 12 | 4 | 8 × 12 + 4 = 100 |
| 1000 mod 7 | 142 | 6 | 7 × 142 + 6 = 1000 |
Take 100 mod 3 as an example. 3 fits into 100 thirty-three times, which makes 99, and 1 is left, so 100 mod 3 = 1. There is a shortcut for 3 and 9: a number gives the same remainder as the sum of its digits. The digits of 100 add up to 1, so 100 mod 3 = 1 and 100 mod 9 = 1. The same trick shows that 500000 mod 9 = 5 without any long division.
Modulo with zero
Zero on the left is easy: 0 mod n = 0 for every n except 0, because zero holds no full n's and leaves nothing behind. 0 mod 3, 0 mod 8 and 0 mod 100 are all 0. Zero on the right has no answer: a mod 0 is undefined, for the same reason that dividing by zero is undefined. That includes 10 mod 0, 1 mod 0 and even 0 mod 0. Python stops with a ZeroDivisionError and JavaScript returns NaN. The calculator shows an error instead of a number.
Modulo of negative numbers
With negative numbers there is no single answer that everybody agrees on. Take −17 mod 5. Both 5 × (−4) + 3 and 5 × (−3) − 2 equal −17, so 3 and −2 are both valid remainders. Which one you get depends on how the quotient is rounded:
- Floored: the quotient is rounded down, and the remainder takes the sign of the divisor. Math texts, Python, Ruby, Excel's MOD function and Wolfram Alpha work this way. −17 mod 5 = 3.
- Truncated: the quotient is cut toward zero, and the remainder takes the sign of the dividend. This is what the % operator does in JavaScript, C, C++, Java, C#, Go, Rust, PHP and Swift. −17 % 5 = −2.
- Euclidean: the remainder is never negative, whatever the signs. Number theory often uses this one. −17 mod 5 = 3.
| Expression | Math, Python | JS, C, JavaJavaScript, C, Java | Euclidean |
|---|---|---|---|
| −17 mod 5 | 3 | −2 | 3 |
| 17 mod −5 | −3 | 2 | 2 |
| −17 mod −5 | −2 | −2 | 3 |
| −8 mod 3 | 1 | −2 | 1 |
| −4 mod 3 | 2 | −1 | 2 |
| −1 mod 12 | 11 | −1 | 11 |
The calculator gives the floored result as its main answer, since that is the one you will find in math classes. When the conventions disagree, it lists all three side by side, and the number line shows both remainders: one measured up from the multiple below a, the other measured down from the multiple above it.
Try −17 mod 5 in the calculator
The modulo operator in programming
Most languages write modulo as the percent sign, so a % n means a mod n. It has the same precedence as multiplication and division, which means a + b % n is read as a + (b % n). Add brackets when you want the sum first. In code, the two most common behaviors look like this:
# Python: sign of the divisor
-17 % 5 # 3
divmod(-17, 5) # (-4, 3)
// JavaScript: sign of the dividend
-17 % 5 // -2
((-17 % 5) + 5) % 5 // 3 (math) The last line is the usual fix when you need a result that is never negative in JavaScript, C or Java. Python and Ruby already behave that way when the divisor is positive.
A few places where modulo shows up in everyday code:
- Even or odd: n % 2 is 0 for even numbers and 1 for odd ones (in JavaScript, −1 for odd negative numbers, so compare with 0).
- Wrapping around a list: (i + 1) % length moves to the next item in a list and jumps back to the start after the last one, which is how carousels and circular buffers work.
- Clock time: 14 mod 12 = 2, so 14:00 is 2 PM. To split 135 minutes into hours and minutes, use 135 div 60 = 2 and 135 mod 60 = 15.
- Days of the week: if today is Monday, the day 100 days from now is 100 mod 7 = 2 days later, a Wednesday.
- Every nth item: i % 3 == 0 picks every third row, for example to stripe a table or batch a job.
Show 100 mod 7 in the calculator
Modular arithmetic
Modular arithmetic is arithmetic on a loop instead of a straight line. A clock is the classic example: count past 12 and you start again at 1. Two numbers that leave the same remainder are called congruent, written a ≡ b (mod n). 26 ≡ 2 (mod 12), because 26 hours from midnight the clock shows the same hour as 2 hours from midnight.
You can take the remainder at any point in a sum or a product and still get the right answer at the end:
For (15 + 28) mod 9, you can add first, 43 mod 9 = 7, or reduce first: 15 mod 9 = 6 and 28 mod 9 = 1, and (6 + 1) mod 9 = 7. The answer is the same either way, and it is how large powers are worked out by hand. To find 7⁵ mod 71, square and reduce as you go: 7² = 49, 49² = 2401 and 2401 mod 71 = 58, then 58 × 7 = 406 and 406 mod 71 = 51. You never touch a number bigger than a few thousand. The calculator also accepts the full power, 7⁵ = 16807, if you would rather type it in.
Check 16807 mod 71 in the calculator
The same ideas run through cryptography. RSA encryption, for instance, raises numbers to huge powers modulo a number hundreds of digits long. Hash tables use key mod table size to pick a slot, and check digits on barcodes, ISBNs and bank account numbers are all modulo calculations.
Modulo with very large numbers
Many calculators and spreadsheets store numbers as floating point, which keeps only about 15 to 16 significant digits. Past 9,007,199,254,740,992 (2⁵³), whole numbers start to round, and the last digits are lost before the modulo is even taken. Ask a floating-point tool for 9007199254740993 mod 2 and you may get 0, even though the number is odd.
This calculator works with exact whole numbers instead, up to 1,000 digits each, and never rounds. An IBAN, for example, is validated by turning it into a long number and checking that it leaves 1 mod 97. The example IBAN GB82 WEST 1234 5698 7654 32 becomes a 28-digit number, which the IBAN example in the calculator checks for you.
Modulo, remainder and modulus
For positive numbers, modulo and remainder give the same result, and people use the words interchangeably. The difference only shows up with negative numbers, as described above. If you want to see the full long division with the quotient and remainder written out, use the long division remainder calculator.
"Modulus" can be confusing. In "a mod n", the modulus is n, the number you divide by. But modulus is also another name for the absolute value of a number, |−5| = 5, and for the length of a complex number. If you searched for a modulus calculator and expected an absolute value, that is a separate operation.
Frequently Asked Questions
What does mod mean in math?
Mod is short for modulo. a mod n is the remainder you get when you divide a by n and keep only whole numbers. For example, 17 mod 5 = 2, because 5 fits into 17 three times (15) and 2 is left over. In most programming languages the same operation is written a % n.
How do you calculate mod on a regular calculator?
Most calculators have no mod key, but four steps do the job. Divide a by n, write down the whole-number part of the answer, multiply it by n, and subtract that from a. For 100 mod 7: 100 ÷ 7 = 14.28, the whole part is 14, 14 × 7 = 98, and 100 − 98 = 2. So 100 mod 7 = 2.
What is 71 mod 3?
71 mod 3 = 2. Dividing 71 by 3 gives a quotient of 23, because 3 × 23 = 69. Subtracting 69 from 71 leaves 2, so the remainder of 71 divided by 3 is 2.
What is 0 mod 3, and what is 10 mod 0?
0 mod 3 = 0. Zero divided by any number is 0 with nothing left over, so 0 mod n is always 0 (0 mod 2, 0 mod 10 and 0 mod 100 are all 0). 10 mod 0 has no answer. Modulo is built on division, and you cannot divide by zero, so a mod 0 is undefined. Python raises a ZeroDivisionError and JavaScript returns NaN.
Why is -8 mod 3 equal to 1 in Python but -2 in JavaScript?
Both answers are valid remainders, because 3 × (−3) + 1 and 3 × (−2) − 2 both equal −8. Python rounds the quotient down to −3, which makes the remainder 1 and gives it the sign of the divisor. JavaScript, C and Java cut the quotient toward zero to −2, which gives a remainder of −2 with the sign of the dividend. Math texts use the Python result.
What is the difference between mod and div?
They are two halves of the same division. Div is the whole-number quotient and mod is what is left over. For 23 and 7, 23 div 7 = 3 and 23 mod 7 = 2, because 23 = 7 × 3 + 2. The calculator shows both: the remainder as the main result and the quotient below it.