Skip to content
Math

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.

Your details

Choose the modular arithmetic operation to perform.
The number being divided, or the base for power mod.
Used in addition, subtraction, multiplication, power mod, and congruence modes.
The modulus (divisor). Must be a non-zero integer.
Compare the mathematical result with the C/Python/JavaScript % result.
Result (mod n)Has a remainder
2
Quotient (floored)3
GCD(a, n)1

17 mod 5 = 2.

  • 17 is 2 more than the nearest multiple of 5 (that multiple is 15).
  • The mathematical modulo is always non-negative: it lies between 0 and |n| - 1 regardless of the sign of a or n.

Repeating pattern: a mod 5

aExpressionRemainderNote
00 mod 50Multiple
11 mod 51
22 mod 52
33 mod 53
44 mod 54

The remainder cycles through 0 to 4 and then repeats every 5 steps.

Formula

amodn=anan[0,n),abmodn via square-and-multiplya \bmod n = a - |n|\left\lfloor \dfrac{a}{|n|} \right\rfloor \in [0,\,|n|),\quad a^b \bmod n \text{ via square-and-multiply}

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

ExpressionMath mod (this calc)JavaScript %Python %Application
17 mod 5222Basic remainder
20 mod 4000Divisibility test
7 mod 10777n > dividend: remainder = n
-17 mod 53-23Negative dividend
17 mod -522-3Negative modulus
3^4 mod 5111Power mod (RSA)
(10 + 5) mod 12333Clock arithmetic
226 mod 10666Barcode 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.

Sources

Written by Dr. Rajiv Menon, PhD Applied Mathematician · Bengaluru, India

Applied mathematician bridging algebraic theory and computational tools for students, engineers, and everyday problem-solvers.

Search 3,500+ calculators

Loading search…