Modulo Calculator (Remainder and mod n)

This calculator reduces a number modulo n using the mathematical convention, so the result is always between zero and n−1 even when the input is negative. It shows the programming remainder beside it, because C, Java and JavaScript all return a value carrying the sign of the dividend and that discrepancy is the single most common bug in code that indexes a cycle. It also performs modular addition, subtraction, multiplication and exponentiation on a second operand, and finds the modular inverse when one exists.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Value aThe number being reduced. Whole numbers only; decimals are rounded.17
Modulus nThe size of the cycle you are wrapping around. It must not be zero.5
Second operationOptional. Each operation reduces both operands first, which gives the same answer with far smaller numbers.(a × b) mod n
Value bThe second operand. For the power option it is the exponent and must not be negative.8

It returns

  • a mod n (least non-negative residue) — Always from 0 up to |n| − 1, whatever the sign of a.
  • Programming remainder a % n — What C, Java and JavaScript return: the sign follows the dividend.
  • Floor quotient ⌊a ÷ |n|⌋
  • Result of the second operation — Reduced modulo |n|. Blank when no operation is selected.
  • Modular inverse of a — The x with a·x ≡ 1 (mod n). Exists only when a and n share no common factor.
  • Congruence statement — How a mathematician would write the reduction.

The formula

a mod n=anan
ab (mod n)n(ab)

In plain text: a mod n = a − |n| · ⌊a ÷ |n|⌋, giving 0 ≤ a mod n < |n|

  • aThe value being reduced (integer)
  • nThe modulus, the size of the cycle (non-zero integer)
  • ⌊ ⌋Floor: round down toward negative infinity (—)

Using the floor rather than truncation is what keeps the residue non-negative for negative inputs. Truncation toward zero gives the C-family remainder instead.

Updated Category Number Theory, Divisors & Number Systems Verified against published test cases Reading time 11 min

What it means to work modulo n

Working modulo n means treating two numbers as the same whenever they differ by a multiple of n. A clock face is the everyday example: 15:00 and 3:00 are the same position because they differ by 12, and 27:00 would be too. The modulus is the size of the cycle, and reducing a number modulo n means finding where it lands on that cycle.

Formally, ab (mod n) means n divides ab. The set of all integers congruent to a given value is a residue class, and there are exactly n of them: everything is congruent to one of 0, 1, …, n−1. Reducing a modulo n means naming which class it belongs to by giving that standard representative, called the least non-negative residue.

What makes this useful rather than merely tidy is that arithmetic survives the reduction. If aa′ and bb′ modulo n, then a+ba′+b′ and abab′ too. So you may reduce whenever you like, before or after adding or multiplying, and the answer is the same. That is what lets you compute 17 × 8 modulo 5 as 2 × 3 = 6 ≡ 1 without ever forming 136, and it is why modular exponentiation of enormous numbers is tractable at all.

Subtraction survives too. Division does not, in general, and that exception is where most of the interesting structure lives.

Two conventions, and the bug that lives between them

The division algorithm says every pair (a, n) with n ≠ 0 has a unique quotient and remainder satisfying a = qn + r with 0 ≤ r < |n|. Getting that non-negative r requires the quotient to be the floor of a/n, rounded toward negative infinity.

Most programming languages do not do that. C, C++, Java, C# and JavaScript all truncate the quotient toward zero, which makes the remainder carry the sign of the dividend. So in those languages -17 % 5 is −2, not 3. Both satisfy a = qn + r; they differ in which of the two valid pairs they pick, because −17 = 5(−4) + 3 and −17 = 5(−3) − 2 are both true.

This is the source of a specific and very common defect. Code that wraps an index around a buffer, a circular list, a hue wheel or a day-of-week table works perfectly until the input goes negative, at which point it produces a negative index. The fix is the idiom ((a % n) + n) % n, which pushes the truncated remainder back into range without disturbing the already-correct positive case.

Not every language behaves this way. Python, Ruby and Haskell make the remainder take the sign of the divisor, so -17 % 5 is 3 there, matching the mathematical convention whenever the modulus is positive. Excel's MOD and SQL's MOD differ from each other on the same input. If you are porting a formula between environments, negative inputs are the first thing to test.

This calculator reports both: the least non-negative residue as the main answer, and the C-family truncated remainder beside it so the difference is visible. The reference table below shows the two side by side across the sign change.

Worked example: 17 mod 5, and (17 × 8) mod 5 the easy way

Reducing 17 modulo 5.

  1. Floor quotient. 17 ÷ 5 = 3.4, and the floor of that is 3.
  2. Remove that many whole moduli. 3 × 5 = 15.
  3. Residue. 17 − 15 = 2. It is in the range 0 to 4, as required.

So 17 ≡ 2 (mod 5), and the whole residue class is …, −8, −3, 2, 7, 12, 17, 22, … — every value differing from 17 by a multiple of 5.

Multiplying without the big number. To find (17 × 8) mod 5, reduce each operand first: 17 ≡ 2 and 8 ≡ 3. Then 2 × 3 = 6, and 6 ≡ 1 (mod 5). Check the long way: 17 × 8 = 136, and 136 = 5 × 27 + 1. ✓ The shortcut used numbers no larger than 6 to get an answer about 136, and the same idea scales: raising a 600-digit number to a 600-digit power modulo a 600-digit modulus is routine precisely because every intermediate stays below the modulus.

A power. Take 2⁴ mod 5. Squaring repeatedly: 2² = 4; 4² = 16 ≡ 1. So 2⁴ ≡ 1 (mod 5). That is Fermat's little theorem in action — for a prime p and any a not divisible by p, ap−1 ≡ 1 (mod p). With p = 5 the exponent is 4, and the theorem predicts the 1 before you compute it.

An inverse. The inverse of 2 modulo 5 is the value x with 2x ≡ 1. Testing: 2×1 = 2, 2×2 = 4, 2×3 = 6 ≡ 1. So the inverse is 3, and dividing by 2 modulo 5 means multiplying by 3. The extended Euclidean algorithm finds this directly rather than by search, which matters when the modulus has hundreds of digits.

Reading the outputs, and when division is possible

The residue names the class, not the number. Getting 2 from 17 mod 5 does not mean 17 has become 2; it means 17 sits in the class whose standard label is 2, alongside 7, 12, −3 and infinitely many others. That distinction matters when you interpret a result: a hash bucket of 2 tells you where an item goes, not what the item was.

The floor quotient tells you how many complete cycles were used. For 17 mod 5 it is 3, meaning three full laps and a partial one. In a date calculation that is the number of complete weeks; in a buffer it is how many times you wrapped.

The modular inverse exists exactly when gcd(a, n) = 1. This is the sharpest departure from ordinary arithmetic. Modulo 5, every non-zero value has an inverse because 5 is prime. Modulo 4, the value 2 has none: 2×0, 2×1, 2×2, 2×3 give 0, 2, 0, 2 and never 1. A modulus that is prime makes the residues a field, in which every non-zero element can be divided by; a composite modulus does not. That single fact explains why prime moduli dominate cryptography and error-correcting codes.

Watch the size of intermediate products. Reducing operands first keeps everything below the modulus, but the product of two values just under n is nearly n². Once n exceeds about 94 million, that product passes the point where whole numbers are stored exactly in double precision, and the answer can be silently wrong. This calculator warns at that threshold. Serious modular arithmetic uses big-integer libraries for exactly this reason.

The mathematical residue against the C-family remainder

Both satisfy a = qn + r. They differ in how the quotient is rounded: the residue uses the floor, the remainder truncates toward zero.
anFloor quotienta mod n (mathematical)a % n (C, Java, JavaScript)
175322
−175−43−2
10071422
−1007−155−2
09000
−112−111−1
712077
2525100

The two columns agree for every non-negative a and differ for every negative a that is not an exact multiple. Python and Ruby produce the fourth column rather than the fifth when the modulus is positive.

Traps in modular arithmetic

  • Assuming your language returns a non-negative remainder. Most do not. Wrap with ((a % n) + n) % n before using the result as an index.
  • Cancelling a common factor in a congruence. From 2x ≡ 2y (mod 6) you cannot conclude x ≡ y (mod 6); you get x ≡ y (mod 3). Cancellation is only valid after dividing the modulus by the gcd.
  • Dividing without checking coprimality. There is no inverse of 2 modulo 4, so no meaning can be given to dividing by 2 there. Check gcd(a, n) = 1 first.
  • Reducing an exponent modulo n. Exponents reduce modulo φ(n), not modulo n, and only when the base is coprime to n. Reducing an exponent the wrong way gives a plausible and wrong answer.
  • Overflowing before reducing. Computing a×b and then taking the remainder can overflow even when the true modular product is small. Reduce first, and use big integers above about 94 million.
  • Confusing the remainder with the quotient in a wrap-around. The residue says where on the cycle you landed; the floor quotient says how many complete cycles you used. Date and page-number calculations usually need both.

Where modular arithmetic actually shows up

Check digits. ISBN-13 and EAN barcodes weight the digits 1, 3, 1, 3, … and choose a final digit making the total ≡ 0 (mod 10). The IBAN standard reduces the rearranged account string modulo 97 and requires the result to be 1. Both catch single-digit errors and most transpositions, and both are pure modular arithmetic.

Calendars. Day-of-week calculations reduce a day count modulo 7, and leap-year rules reduce the year modulo 4, 100 and 400. The Gregorian rule is exactly three congruence tests combined.

Cryptography. RSA is modular exponentiation and nothing else: encryption is me mod n and decryption is cd mod n, with d the modular inverse of e modulo φ(n). Diffie-Hellman and elliptic-curve schemes are built on the same operations over carefully chosen moduli. The prime checker matters here because the security rests on the modulus being a product of large primes.

Hashing and cycling. Mapping a key into a table of size n is a modulo operation, and choosing n prime spreads keys more evenly when the inputs have structure. Circular buffers, ring counters and animation loops all wrap with the same operation.

The mathematical neighbours are close by. The residue is the r of the division algorithm, which shows the digit-by-digit working. Whether an inverse exists is a question for the greatest common factor, since the extended form of Euclid's algorithm produces the inverse as a by-product. Converting a number between bases is repeated reduction modulo the new base, which is what the base conversion calculator does. And solving simultaneous congruences with coprime moduli is the Chinese remainder theorem, which needs the least common multiple to state the period of the combined solution.

Frequently asked questions

What is 17 mod 5?

Two. Five goes into 17 three whole times, using 15, and 17 − 15 = 2 is left. The result always lies between 0 and one less than the modulus, so for a modulus of 5 the only possible answers are 0, 1, 2, 3 and 4. Every number differing from 17 by a multiple of 5 — 22, 12, 7, −3 — gives the same answer, because they all sit in the same residue class.

Why does my programming language give a negative result for -17 % 5?

Because C, Java, C# and JavaScript truncate the quotient toward zero, which makes the remainder take the sign of the dividend: −17 = 5(−3) − 2, so the answer is −2. The mathematical convention uses the floor instead, giving −17 = 5(−4) + 3 and a residue of 3. Both satisfy a = qn + r. Use ((a % n) + n) % n to get the non-negative version; Python and Ruby already return it for positive moduli.

What is a modular inverse and when does one exist?

It is the value x with a·x ≡ 1 (mod n), which plays the role of dividing by a. It exists exactly when a and n share no common factor greater than one. Modulo 5, the inverse of 2 is 3 because 2×3 = 6 ≡ 1. Modulo 4, the value 2 has no inverse at all, since 2 times anything is even and can never be congruent to 1. The extended Euclidean algorithm finds the inverse directly.

Can I reduce the exponent in a modular power?

Not modulo n. Exponents reduce modulo φ(n), Euler's totient, and only when the base is coprime to n. For a prime p that gives Fermat's little theorem: ap−1 ≡ 1 (mod p), so exponents can be taken modulo p−1. Reducing an exponent modulo n instead produces a wrong answer that looks entirely plausible, which is why this is a favourite examination trap.

Is modulo the same as the remainder?

They coincide for non-negative values and diverge for negative ones. “Remainder” usually refers to whatever your division operator returns, which in the C family carries the sign of the dividend. “Modulo” in mathematics means the least non-negative residue, always between 0 and |n|−1. When precision matters, say which convention you mean rather than relying on the word.

Can the modulus be negative?

It can be entered as one, and it generates the same residue classes as its magnitude, so this calculator reduces modulo |n| and returns a non-negative residue. Languages differ here too: JavaScript's 17 % -5 is 2 while Python's is −3. Because nothing is gained by a negative modulus, the safe habit is to use the positive one and reduce the sign question to the dividend alone.

Why do hash tables and cryptography prefer prime moduli?

Because a prime modulus makes every non-zero residue invertible, so the residues form a field and division always works. For hashing, a prime also avoids the case where the modulus shares a factor with structure in the keys, which would collapse many keys onto the same bucket. For cryptography, the invertibility is what makes decryption possible at all: the private exponent is a modular inverse.

How do I check whether two numbers are congruent?

Subtract them and test whether the modulus divides the difference. 38 and 17 are congruent modulo 7 because 38 − 17 = 21 and 7 divides 21. Equivalently, reduce both and compare the residues: 38 ≡ 3 and 17 ≡ 3. The subtraction test is usually faster by hand, and it is the definition, whereas comparing residues is a consequence of it.

References

  • An Introduction to the Theory of Numbers, 6th edition (congruences and residue classes) — Oxford University Press (Hardy and Wright)
  • Concrete Mathematics: A Foundation for Computer Science, 2nd edition (floor, mod and the sign conventions) — Addison-Wesley (Graham, Knuth and Patashnik)
  • Handbook of Applied Cryptography (modular arithmetic, inverses and exponentiation)CRC Press (Menezes, van Oorschot and Vanstone)