Modulo Calculator: Remainders, Negative Numbers and Mod Powers
By FindMySum · Last reviewed
Modulo is the remainder after division: 17 mod 5 = 2 because 5 goes into 17 three times with 2 left over. It is one of the most used operations in programming (even and odd tests, wrapping around arrays, hashing, clock arithmetic, check digits) and the foundation of number theory and cryptography. This calculator does three jobs: division with remainder under every common convention, modular powers, and modular inverses.
Division with remainder
For whole numbers a (the dividend) and n (the divisor, not zero) there is a quotient q and a remainder r with
a = q × n + r
The remainder is smaller than the divisor in size. When a and n are positive there is only one such pair, and every calculator and language agrees: 17 = 3 × 5 + 2, so the quotient is 3 and 17 mod 5 = 2. That is the calculator's default example.
Negative numbers: three conventions
When a or n is negative, there are two valid remainders, one positive and one negative, and different tools pick different ones. The difference comes from how they round the quotient:
| Convention | Quotient | Remainder sign | −7 mod 3 | 7 mod −3 | Used by |
|---|---|---|---|---|---|
| Truncated | rounded toward 0 | same as a | −1 | 1 | JavaScript, C, C++, Java, C#, Go, Rust |
| Floored | rounded down | same as n | 2 | −2 | Python, Ruby, Excel MOD, Google Sheets |
| Euclidean | chosen so r ≥ 0 | never negative | 2 | 1 | Number theory textbooks |
All three satisfy a = q × n + r. For −7 and 3, truncation gives q = −2, so r = −7 − (−6) = −1; flooring gives q = −3, so r = −7 − (−9) = 2.
The calculator shows all three side by side, with each quotient, and headlines the Euclidean remainder because it is the one mathematics means by "a mod n". If you are checking code, read the row for your language.
Decimals
The floored and truncated definitions work for any real numbers: 5.5 mod 2 = 1.5 and −5.5 mod 2 = 0.5 (floored) or −1.5 (truncated). Programming languages store decimals in binary floating point, which is why 0.3 % 0.1 in JavaScript gives 0.09999999999999998 instead of 0. The calculator works in exact decimal arithmetic, so typed decimals give exact answers.
Very large integers
Whole numbers are handled with arbitrary-precision integers, so a 30-digit dividend such as 123456789012345678901234567890 mod 9876543210 gives the exact remainder 1562499990, where a spreadsheet would lose the last digits. Inputs can have up to 400 digits.
Modular exponentiation
Computing a^b mod n by working out a^b first is hopeless for the numbers used in cryptography, where a^b would have more digits than there are atoms in the universe. Square-and-multiply avoids it:
- Write the exponent b in binary.
- Start with result = 1 and square = a mod n.
- For each bit from the right: if the bit is 1, multiply result by square (mod n); then square the square (mod n).
For 4^13 mod 497, 13 is 1101 in binary. The squares are 4, 16, 256 and 256² mod 497 = 429. Multiplying the ones for bits 0, 2 and 3 gives 4 × 256 × 429 mod 497 = 445. The breakdown table lists every step. The number of steps grows with the number of digits in b, not with b itself, which is why a 300-digit exponent is no problem.
Modular inverse
The inverse of a modulo n is the number x for which a × x leaves remainder 1: a × x ≡ 1 (mod n). It plays the role of division in modular arithmetic. The inverse exists exactly when a and n have no common factor other than 1.
The extended Euclidean algorithm finds it. Euclid's algorithm repeatedly replaces (a, n) by (n, a mod n) until the remainder is 0; the extended version keeps track of how each remainder is built from a and n, so when the remainder reaches 1 it has found s and t with s × a + t × n = 1. Then s mod n is the inverse.
The classic RSA teaching example uses n = 3120 (that is, (61 − 1) × (53 − 1)) and the public exponent 17. The private exponent is the inverse of 17 mod 3120, which the calculator finds as 2753: 17 × 2753 = 46,801 = 15 × 3120 + 1.
Where modulo shows up
- Even and odd: n mod 2 is 0 for even numbers.
- Clock and calendar arithmetic: 30 hours after 22:00 is (22 + 30) mod 24 = 4:00; the day of the week is a mod 7 calculation.
- Wrapping indexes: moving through a list of length L, the next position is (i + 1) mod L.
- Check digits: ISBN-10, credit card Luhn checks and Canadian social insurance numbers all use remainders.
- Hashing: a hash table puts a key in bucket hash(key) mod size.
- Cryptography: RSA, Diffie–Hellman and elliptic-curve systems are all modular arithmetic on very large numbers.
Common mistakes
- Assuming % in your language returns a non-negative result for negative numbers.
- Testing
n % 2 == 1to find odd numbers in a truncating language. - Using floating point for money or large integers before taking a remainder.
- Expecting a modular inverse when a and n share a factor.
More questions
What is 0 mod n?
0, for any non-zero n, because 0 = 0 × n + 0. Zero divides evenly by everything, so every convention agrees.
What is n mod 0?
Undefined, like division by zero. Some languages throw an error, JavaScript returns NaN, and Excel shows #DIV/0!. The calculator asks for a non-zero divisor.
Why does Excel's MOD give a different answer from my code?
Excel's MOD uses the floored convention, so the result has the sign of the divisor: MOD(−7, 3) = 2. JavaScript, C, C++, Java and C# use truncation, so −7 % 3 = −1. Python matches Excel. When you port a formula between the two, negative inputs are where it breaks.
How do I check if a number is even with modulo?
A number is even when n mod 2 = 0. In truncating languages, test for 0 (or for not 0) rather than for 1, because −3 % 2 is −1, not 1, and a test for 1 silently treats negative odd numbers as even.
What does congruent mod n mean?
Two numbers are congruent mod n when they leave the same Euclidean remainder, or equivalently when their difference is a multiple of n. 17 ≡ 2 (mod 5) and −3 ≡ 2 (mod 5) because 17 − 2 = 15 and −3 − 2 = −5 are both multiples of 5.
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.