Modulo Calculator
Enter a and n to get a mod n, the quotient behind it, and the answer each convention gives when one of the numbers is negative. Truncated (C, Java, JavaScript), floored (Python, Ruby, Excel) and Euclidean (number theory) sit side by side.
- All three conventions at once
- Per-language answers
Modulo Operator
LiveAll three conventions, side by side.
- Truncated — C, Java, JS
- —
- Floored — Python, Ruby
- —
- Euclidean — never negative
- —
- Conventions that agree
- —
The mod calculation, step by step
The three conventions differ in one place only: how each rounds the quotient. Everything else follows from that single choice.
The three conventions compared
On the number line
What each language returns
Modular exponentiation: ab mod n
What Does a mod n Mean?
Modulo is the operation that returns the remainder of a division. Writing x mod y = r says that dividing x by y leaves r behind, and the r is the answer the operation hands back.
The operation has 4 named components. The dividend x is the number being divided, the divisor y is the number dividing it — called the modulus in modular arithmetic — the quotient q counts the whole copies that fit, and the remainder r is what modulo returns. The word comes from the Latin modus, meaning a measure.
Clock arithmetic runs on modulo 12 and shows the operation working without any notation at all. A clock reading 11 pm, advanced by 8 hours, lands on 7 am rather than 19 — the count wraps at 12 and starts again.
11 + 8 = 19, and 19 mod 12 = 7.
Subtract 12 once and the count is back inside the face. Minutes and seconds run on modulo 60 in the same way, which is why 90 seconds is written as 1 minute 30 seconds rather than left as 90.
Two numbers that leave the same remainder are called congruent modulo n, written a ≡ b (mod n). 24 and 34 are congruent modulo 10, since both leave 4. The congruence relation has 4 equivalent forms, and each one states the same fact:
a ≡ b (mod n)a mod n = b mod nn | (a − b), read as n divides a − ba = b + knfor some integer k
9 ≡ 21 (mod 6) holds under all 4: both leave 3, the difference 21 − 9 = 12 is a multiple of 6, and 21 = 9 + 2 × 6. Congruence is what turns modulo from a single calculation into modular arithmetic — a full system of addition, subtraction, multiplication and exponentiation that wraps around a fixed modulus.
How to Use the Modulo Calculator
To use the modulo calculator, type the dividend into the first box, type the modulus into the second, and read the result. The answer and the working appear together in 4 steps:
- Enter a, the dividend. Negative values and decimals are both accepted, and 7.5 mod 2 returns 1.5.
- Enter n, the modulus. The headline reports the Euclidean answer, which never comes out negative.
- Compare the three conventions in the stat tiles. The tiles read identically for positive inputs and split apart the moment a negative appears.
- Open the working panel for the number line, the per-language table, and modular exponentiation for large powers.
The modulo calculator reports 5 values for every pair: the Euclidean result, the truncated result, the floored result, the quotient each convention implies, and the identity a = n × q + r written out with your numbers. The modular exponentiation panel adds ab mod n for exponents far past what a plain power could hold, and returns the modular inverse for a negative exponent.
Other tools reach the same answer through different names.
Excel and Google Sheets use
MOD(number, divisor), which floors.
Desmos uses mod(a, n).
The TI-84 Plus keeps
remainder( under MATH ▸ NUM.
Casio scientific models ship no mod key, so compute
a − n × Intg(a ÷ n) instead.
Wolfram Alpha and Symbolab accept
Mod[a, n] and
a mod n as typed.
Modulo Formula
The modulo formula is r = a − n × ⌊a ÷ n⌋, which subtracts every whole copy of the modulus that fits inside the dividend. The floor brackets mark floor division — division with the fractional part thrown away.
a = n × q + r the division algorithm q = ⌊a ÷ n⌋ floor division: round down to a whole number r = a − n × q the modulo result 250 = 24 × 10 + 10 since ⌊250 ÷ 24⌋ = ⌊10.4166…⌋ = 10 250 mod 24 = 10Calculating the modulo by hand takes 4 moves: divide, round down, multiply back, subtract. Dividing 250 by 24 gives 10.4166, rounding down gives 10, multiplying back gives 240, and subtracting leaves 10.
Modular arithmetic extends the formula to 4 operations that all commute with the modulo step. Reducing early keeps every intermediate value small, which is what makes the arithmetic practical on numbers of any size:
(A + B) mod C = (A mod C + B mod C) mod C (A − B) mod C = (A mod C − B mod C) mod C (A × B) mod C = (A mod C × B mod C) mod C A^B mod C = ((A mod C)^B) mod C with A = 11, B = 7, C = 4: (11 + 7) mod 4 = 18 mod 4 = 2 and (3 + 3) mod 4 = 6 mod 4 = 2 ✓ (11 × 7) mod 4 = 77 mod 4 = 1 and (3 × 3) mod 4 = 9 mod 4 = 1 ✓ (11 ^ 7) mod 4 = 19487171 mod 4 = 3 and 3^7 mod 4 = 2187 mod 4 = 3 ✓Modular exponentiation is the operation that carries the most weight outside the classroom. Computing 2^100 mod 3 directly would need a 31-digit number; squaring and reducing at every step keeps the running value under 3 and finishes in 7 squarings. RSA and the Diffie-Hellman key exchange are built on exactly that gap — the exponentiation is quick, and undoing it without the private key is not. Fermat's little theorem and Euler's totient function shrink the exponent further before the work starts, and the system of congruences solver splits one large modulus into several small ones.
Example Modulo Calculation
Calculating 17 mod 3 returns 2. The number line shows where the answer comes from: 3 fits into 17 five times, reaching 15, and the gap from 15 to 17 is the remainder.
The 6 steps behind that answer are the same 6 for any pair. Choose the dividend 17. Choose the divisor 3. Divide to get 5.6667. Round down to the quotient 5. Multiply 5 × 3 = 15. Subtract 17 − 15 = 2.
A result larger than the divisor never happens. Every remainder falls in the range 0 to n − 1, so modulo 3 returns 0, 1 or 2 and nothing else. A modulus larger than the dividend returns the dividend untouched: 1 mod 2 = 1, since 2 fits into 1 zero times and the whole 1 stays behind.
| Calculation | Quotient | Result | Why |
|---|---|---|---|
| 1 mod 1 | 1 | 0 | 1 divides everything exactly |
| 1 mod 2 | 0 | 1 | modulus larger than dividend |
| 5 mod 2 | 2 | 1 | 5 = 2 × 2 + 1 |
| 5 mod 3 | 1 | 2 | 5 = 3 × 1 + 2 |
| 6 mod 3 | 2 | 0 | 6 is a multiple of 3 |
| 10 mod 3 | 3 | 1 | 10 = 3 × 3 + 1 |
| 11 mod 4 | 2 | 3 | 11 = 4 × 2 + 3 |
| 17 mod 3 | 5 | 2 | 17 = 3 × 5 + 2 |
| 100 mod 3 | 33 | 1 | 100 = 3 × 33 + 1 |
| 100 mod 7 | 14 | 2 | 100 = 7 × 14 + 2 |
| 250 mod 24 | 10 | 10 | 250 = 24 × 10 + 10 |
| 496 mod 4 | 124 | 0 | 496 is a multiple of 4 |
Negative Inputs and the Three Conventions
Modulo with negative numbers produces 2 different answers, and both are correct under their own convention. −7 mod 3 returns −1 in C, Java and JavaScript, and 2 in Python, Ruby and mathematics.
The split comes from 1 decision: which way to round the quotient. Rounding −7 ÷ 3 = −2.333 toward zero gives −2, and −7 − 3 × (−2) = −1. Rounding down gives −3, and −7 − 3 × (−3) = 2. Both satisfy a = n × q + r, so the identity alone cannot pick a winner.
| Convention | Quotient rounded | −7 mod 3 | 7 mod −3 | Remainder takes |
|---|---|---|---|---|
| Truncated | toward zero | −1 | 1 | sign of the dividend |
| Floored | toward −∞ | 2 | −2 | sign of the divisor |
| Euclidean | so r ≥ 0 | 2 | 1 | never negative |
Number theory uses the Euclidean convention, since a statement such as x ≡ 2 (mod 5) has to name a single residue rather than a choice of two. Every congruence class modulo 5 is then written with one of 0, 1, 2, 3 or 4, and the system of congruences solver normalises its answers the same way.
Modulo in Programming and the % Operator
The % symbol denotes modulo in most programming languages, borrowed from the percent sign rather than from any mathematical notation. C introduced the convention and Java, JavaScript, C++, C#, Go, Rust, PHP and Swift kept it, which is why those 8 languages all return −1 for −7 % 3.
| Language | Expression | −7 mod 3 | Convention |
|---|---|---|---|
| Python | -7 % 3 | 2 | floored |
| Ruby | -7 % 3 | 2 | floored |
| Excel | MOD(-7, 3) | 2 | floored |
| JavaScript | -7 % 3 | −1 | truncated |
| C / C++ | -7 % 3 | −1 | truncated |
| Java | -7 % 3 | −1 | truncated |
| Java | Math.floorMod(-7, 3) | 2 | floored |
| Rust | -7 % 3 | −1 | truncated |
| Rust | (-7i32).rem_euclid(3) | 2 | Euclidean |
| Python | math.fmod(-7, 3) | −1 | truncated |
Large numbers need a wider integer type than the language default.
JavaScript ships BigInt, so
(2n ** 100n) % 3n stays exact where the
plain number type loses precision above 2^53.
Java provides BigInteger, whose
mod() returns a non-negative result while
remainder() truncates.
Python integers grow without limit already.
C and C++ reach for the GNU Multiple Precision
Arithmetic Library (GMP), where
mpz_mod never returns a negative value.
Modulo by a power of 2 compiles down to a bitmask, which is why
x % 256 and
x & 255 agree for non-negative x and
the second runs faster. Hexadecimal and binary conversion lean on the
same operation: repeatedly taking mod 16 and dividing by 16 peels off
one hex digit at a time.
The operation earns its keep in 6 places outside arithmetic:
- Wrapping indexes. Circular buffers, carousels, ring queues and hue values all stay in range through modulo.
- Cycles and scheduling. Clock time runs on modulo 12 and 24, minutes and seconds on modulo 60, weekdays on modulo 7.
- Check digits. GTIN, UPC and EAN barcodes use modulo 10, ISBN and ISSN use modulo 11, and IBAN bank account numbers use modulo 97 to catch a mistyped digit.
- Hashing. A hash reduced modulo the table size picks the bucket, which is why table sizes are often prime.
- Cryptography. RSA, Diffie-Hellman and elliptic curve schemes are modular exponentiation over large moduli, with the modular inverse and the greatest common divisor doing the key work.
- Ciphers. The Caesar shift runs on modulo 26, one step per letter of the alphabet.
Testing x % n === 0 is the standard
divisibility check, and the
divisibility rule checker
covers the shortcuts that reach the same answer from the digits.
Common Modulo Mistake
The common modulo mistake is assuming % returns a non-negative result. It does in Python and Excel, and it does not in JavaScript, C, Java, Go or Rust — where a negative dividend produces a negative answer and every array index built on it lands out of bounds.
The failure is quiet. Wrapping an index around a buffer of 8 works for every positive value and breaks at exactly one position:
// JavaScript const i = -1; const idx = i % 8; // -1, not 7 arr[idx]; // undefined
// JavaScript const i = -1; const idx = ((i % 8) + 8) % 8; // 7 arr[idx]; // the last item
The double-modulo idiom
((a % n) + n) % n costs 1 extra division
and removes the whole category of bug. The first operation may land
negative, adding n pushes the value into range, and the second reduces
it back. Write it even when the input looks certain to be positive.
Java offers Math.floorMod and Rust offers
rem_euclid as built-in equivalents.
Three further slips follow from the same root:
- Reading fmod as %.
math.fmod(-7, 3)returns −1 in Python while-7 % 3returns 2, so the two disagree inside a single language. - Applying % to floats and trusting the last digit.
Floating point makes
0.3 % 0.1return a value near 0.0999 rather than 0. - Forgetting the zero guard. A modulus that reaches 0 raises in Python and Java, returns NaN in JavaScript, and is undefined in C.
Check any modulo answer against the identity a = n × q + r, and confirm the remainder sits below the modulus. The remainder calculator returns the same value for positive whole numbers, and the long division tableau tool draws the subtractions that produce it.