Modulo Calculator
Compute a mod n in any mode: basic remainder, modular addition, modular subtraction, modular multiplication, or power mod (a^b mod n). Choose a mode, enter your numbers, and get the non-negative mathematical result with a full show-your-work breakdown.
Formula
Worked example
17 mod 5: floor(17/5) = 3, so 17 - 5 x 3 = 2. Answer: 2. For -17 mod 5: floor(-17/5) = -4, so -17 - 5 x (-4) = 3. For power mod: 3^4 mod 5 = 81 mod 5 = 1 (fast squaring: 3^2=9, 9 mod 5=4; 4^2=16, 16 mod 5=1).
What the modulo operation means
The modulo of a divided by n is the amount left over after you remove as many whole copies of n as possible from a. If a is 17 and n is 5, three copies of 5 make 15, leaving a remainder of 2, so 17 mod 5 equals 2. The result always lies in the range 0 to |n| - 1, which is why modulo is the engine behind clock arithmetic, hashing, cycling through array indices, check digits, and divisibility tests.
Negative numbers and the sign convention
Mathematicians define a mod n to be non-negative, using floored division: a - n times floor(a / n). Under that rule, -17 mod 5 is 3. Many programming languages disagree. JavaScript and C use truncated division (rounding toward zero), so -17 % 5 yields -2, keeping the sign of the dividend. Python follows the mathematical convention and gives 3. This calculator always returns the non-negative mathematical result. Toggle the language comparison to see what JavaScript and Python return side by side.
Modular arithmetic operations
Modular arithmetic extends beyond a single remainder. In modular addition, you add two numbers and take the mod, wrapping the result into the range 0 to n - 1, exactly like hours on a 12-hour clock: 10 + 5 = 15 reduces to 3 (mod 12). Modular subtraction works the same way, and the floored mod ensures the answer stays non-negative even when the difference is negative. Modular multiplication reduces the product: 7 times 8 mod 12 = 56 mod 12 = 8. These three operations are the building blocks of cryptography, digital signatures, and error-correcting codes.
Power mod and cryptography
Power mod computes a^b mod n without ever evaluating a^b directly, which would overflow for large exponents. The repeated-squaring (square-and-multiply) algorithm works by halving the exponent at each step: to find 3^13 mod 7, write 13 in binary as 1101 and square-and-multiply through each bit. The result is always a single digit in the range 0 to n - 1. Power mod is the core computation in RSA encryption, the Diffie-Hellman key exchange, and primality tests such as the Miller-Rabin test.
Real-world applications of modulo
Modulo appears in check-digit schemes for ISBN-13 (mod 10), IBAN bank codes (mod 97), and GTIN barcodes (mod 10). It underlies even-versus-odd checks (a mod 2), day-of-week calculations (days mod 7), hash-table bucket assignment (hash mod buckets), and color palette cycling in graphics. In competitive programming and finance, modular arithmetic prevents integer overflow when multiplying large numbers by keeping every intermediate result in a bounded range.
Modulo quick-reference
| Expression | Math mod (this calc) | JavaScript % | Python % | Application |
|---|---|---|---|---|
| 17 mod 5 | 2 | 2 | 2 | Basic remainder |
| 20 mod 4 | 0 | 0 | 0 | Divisibility test |
| 7 mod 10 | 7 | 7 | 7 | n > dividend: remainder = n |
| -17 mod 5 | 3 | -2 | 3 | Negative dividend |
| 17 mod -5 | 2 | 2 | -3 | Negative modulus |
| 3^4 mod 5 | 1 | 1 | 1 | Power mod (RSA) |
| (10 + 5) mod 12 | 3 | 3 | 3 | Clock arithmetic |
| 226 mod 10 | 6 | 6 | 6 | Barcode check digit |
Mathematical (non-negative) result versus the JavaScript % and Python % conventions, plus common applications.
Frequently asked questions
Why is -17 mod 5 equal to 3 and not -2?
The mathematical modulo uses floored division, which forces the result into the range 0 to n - 1. Since -17 = 5 x (-4) + 3, the remainder is 3. JavaScript and C instead use truncated division, returning -2, which keeps the sign of the dividend. Python follows the mathematical convention and returns 3.
What happens when I divide by zero?
a mod 0 is undefined, just like ordinary division by zero. There is no whole number of copies of zero to subtract, so the calculator returns no result and asks you to enter a non-zero modulus.
Can I use modulo with decimals?
Yes. The same definition applies: 7.5 mod 2 is 1.5, because floor(7.5 / 2) = 3 and 7.5 - 2 x 3 = 1.5. Note that power mod and congruence checks work best with integers; floating-point rounding can introduce small errors.
What is power mod used for?
Power mod (a^b mod n) is the core computation in RSA encryption and the Diffie-Hellman key exchange. Computing a^b directly overflows for large exponents, but the repeated-squaring algorithm keeps every intermediate value below n, so it works on arbitrarily large exponents in milliseconds.
What does it mean for two numbers to be congruent modulo n?
Two integers a and b are congruent modulo n (written a ≡ b mod n) if they leave the same remainder when divided by n, or equivalently if (a - b) is divisible by n. For example, 17 ≡ 2 (mod 5) because both leave remainder 2, and 17 - 2 = 15 is divisible by 5.
How does modulo work in check-digit schemes?
Check digits use modulo to detect transcription errors. An ISBN-13 appends a digit so the weighted sum of all 13 digits is divisible by 10 (mod 10 = 0). IBAN validation converts letters to numbers and checks that the full string mod 97 equals 1. A single digit swap or transposition changes the checksum, flagging the error.