How to use the modulo calculator
- Type the number being divided in Dividend a.
- Type the number you divide by in Divisor n.
- Read the top line, Floored modulo (maths, Python %), which shows
a mod n = r. - Compare it with the Truncated remainder and the Euclidean remainder if you are working with negative numbers or code.
- Use the Check box to confirm a = n × q + r.
What it does and when to use it
The modulo operation gives the remainder after division. 17 mod 5 = 2 because 17 = 5 × 3 + 2. You use it for clock times (15:00 is 3 o’clock because 15 mod 12 = 3), for working out days of the week, for checking even and odd numbers, and for check digits.
With positive numbers every convention agrees. With negatives they do not, and that catches out many programmers and students. This tool lays the three rules next to each other so you can pick the one your language or textbook uses. For whole-number division in binary, see the binary calculator; for common factors, the GCD and LCM calculator uses repeated mod steps.
How it works
Every rule writes a = n × q + r. They differ only in how the quotient q is chosen from a ÷ n:
| Rule | Quotient q | Sign of r |
|---|---|---|
| Floored (maths, Python) | floor(a ÷ n), rounded down | same as n |
| Truncated (C, Java, JavaScript) | a ÷ n with the decimals cut off | same as a |
| Euclidean | floor if n > 0, ceiling if n < 0 | never negative |
Then r = a − n × q. The numbers are stored as exact fractions, so long dividends and decimals do not lose precision.
Step by step for −17 mod 5:
- Divide: −17 ÷ 5 = −3.4.
- Floored: round down to −4. r = −17 − 5 × (−4) = −17 + 20 = 3.
- Truncated: cut off the decimals to −3. r = −17 − 5 × (−3) = −17 + 15 = −2.
- Euclidean: n is positive, so it matches floored: 3.
The tool shows -17 mod 5 = 3 and the check -17 = 5 × -4 + 3.
Worked examples
Positive numbers. 17 and 5 give 17 mod 5 = 2, with quotient 3. All three rules agree.
Negative dividend. −17 and 5 give 3 floored, but Truncated remainder (C, Java, JavaScript %): -2.
Negative divisor. 17 and −5 give 17 mod -5 = -3 floored, 2 truncated, and a Euclidean remainder of 2.
A 30-digit number. 123456789012345678901234567890 mod 97 gives mod 97 = 52, exactly.
Limits and tips
- The Euclidean remainder is always 0 or more, which makes it safe for array indexes and wrapping around a list.
- In JavaScript you can get the floored result with ((a % n) + n) % n when n is positive.
- To turn the remainder into a fraction such as 17/5 = 3 2/5, use the fraction calculator.
- Nothing leaves your browser.
Frequently asked questions
Why do Python and JavaScript give different answers for −17 % 5?
Which answer should I use in maths class?
Can I use decimals?
How big can the numbers be?
What about dividing by zero?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.