Modulo Calculator
Test cases replayed : 4 of 4 match · Methodology · No outside source listed
Enter a number a and a modulus n to get a mod n and the quotient.
Result
a mod n (floored, takes the sign of n)
Truncated remainder (takes the sign of a, like % in C, Java, JavaScript)
- Floored quotient ⌊a ÷ n⌋
Estimate for planning only, not tax, legal, financial or medical advice. This calculator uses the method explained on this page.
This browser did not allow copying. The sentence is in the box below, already selected: copy it with your keyboard or the menu.
How to use this calculator
Fill in the fields in this order; each step takes you to its field.
- Number a (dividend): enter a number.
- Modulus n (divisor, not 0): enter a number.
- Read the result: a mod n (floored, takes the sign of n); the panel also shows Truncated remainder (takes the sign of a, like % in C, Java, JavaScript) and Floored quotient ⌊a ÷ n⌋.
How this calculator works
The modulo operation gives the remainder r in a = n·q + r. In the floored definition (Knuth), q = ⌊a ÷ n⌋ and r = a − n⌊a ÷ n⌋, so r has the sign of n: −17 mod 5 = 3.
Many programming languages (C, Java, JavaScript) truncate the quotient toward zero instead, so their remainder has the sign of a: −17 % 5 = −2. Python's % uses the floored rule.
For positive numbers both definitions agree: 17 mod 5 = 2.
The formula
| Symbol | Meaning |
|---|---|
| Number a (dividend) | |
| Modulus n (divisor, not 0) | |
| a mod n (floored, takes the sign of n) | |
floor(x) | the largest whole number not above x. |
Worked example
| Number a (dividend) | 17 |
|---|---|
| Modulus n (divisor, not 0) | 5 |
| Truncated remainder (takes the sign of a, like % in C, Java, JavaScript) | 2 |
|---|---|
| Floored quotient ⌊a ÷ n⌋ | 3 |
a mod n (floored, takes the sign of n) 2
Each step uses the unrounded value of the step before it; figures on this page are rounded for display.
Frequently asked questions
What is 17 mod 5?
2, because 17 = 5 × 3 + 2.
What is −17 mod 5?
3 with the mathematical definition (−17 = 5 × (−4) + 3); −2 with the truncated remainder of C or JavaScript.
What is modulo used for?
Clock arithmetic (14 mod 12 = 2 o'clock), checking even and odd numbers (n mod 2), check digits and hashing.