Modular Arithmetic · Operators

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

Live
Try an example
Results

All three conventions, side by side.

Euclidean result (mathematics)
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
Whole numbers only, and a and n must be whole too. A negative exponent returns the modular inverse raised to that power.
Result

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.

x mod y = r when x = qy + r for some integer q 17 mod 3 = 2 since 17 = 3 × 5 + 2

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.

1 2 3 4 5 6 7 8 9 10 11 12

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 n
  • n | (a − b), read as n divides a − b
  • a = b + kn for 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:

  1. Enter a, the dividend. Negative values and decimals are both accepted, and 7.5 mod 2 returns 1.5.
  2. Enter n, the modulus. The headline reports the Euclidean answer, which never comes out negative.
  3. Compare the three conventions in the stat tiles. The tiles read identically for positive inputs and split apart the moment a negative appears.
  4. 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 = 10

Calculating 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.

0 3 6 9 12 15 18 21 17 5 × 3 = 15 remainder 2

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 1101 divides everything exactly
1 mod 201modulus larger than dividend
5 mod 2215 = 2 × 2 + 1
5 mod 3125 = 3 × 1 + 2
6 mod 3206 is a multiple of 3
10 mod 33110 = 3 × 3 + 1
11 mod 42311 = 4 × 2 + 3
17 mod 35217 = 3 × 5 + 2
100 mod 3331100 = 3 × 33 + 1
100 mod 7142100 = 7 × 14 + 2
250 mod 241010250 = 24 × 10 + 10
496 mod 41240496 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.

−9 −6 −3 0 3 −7 floor: q = −3 → r = 2 truncate: q = −2 → r = −1

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
Truncatedtoward zero−11sign of the dividend
Flooredtoward −∞2−2sign of the divisor
Euclideanso r ≥ 021never 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 % 32floored
Ruby-7 % 32floored
ExcelMOD(-7, 3)2floored
JavaScript-7 % 3−1truncated
C / C++-7 % 3−1truncated
Java-7 % 3−1truncated
JavaMath.floorMod(-7, 3)2floored
Rust-7 % 3−1truncated
Rust(-7i32).rem_euclid(3)2Euclidean
Pythonmath.fmod(-7, 3)−1truncated

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:

Wrong
// JavaScript
const i = -1;
const idx = i % 8;      // -1, not 7
arr[idx];               // undefined
Right
// 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 % 3 returns 2, so the two disagree inside a single language.
  • Applying % to floats and trusting the last digit. Floating point makes 0.3 % 0.1 return 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.

FAQs

What is the difference between modulo and remainder?

Modulo and remainder agree on positive numbers and separate on negatives. Both give 17 mod 3 = 2. For −7 and 3, the % operator in C, Java and JavaScript returns −1, while modulo in mathematics returns 2. The word remainder names whatever a language's % gives back, and modulo names the non-negative value that modular arithmetic uses.

How to calculate modulo division?

To calculate modulo division, subtract the divisor from the dividend until what is left is smaller than the divisor. Starting from 17 and taking away 3 five times leaves 2, so 17 mod 3 = 2. The faster route divides once: 17 ÷ 3 gives a quotient of 5 and a remainder of 2.

What are the components of modulo division?

The components of modulo division are the dividend, the divisor, the quotient and the remainder. In 17 mod 3 = 2, the 17 is the dividend, the 3 is the divisor — also called the modulus — the 5 is the quotient, and the 2 is the remainder, which is the answer the operation returns.

How much is 17 mod 3?

17 mod 3 equals 2, since dividing 17 by 3 gives a quotient of 5 and a remainder of 2. Written out in full, 17 = 3 × 5 + 2. Every number of the form 17 + 3k shares that remainder, which makes 17, 20, 23 and 14 all congruent modulo 3.

Can you calculate modulo by zero?

No, modulo by zero is undefined, since no quotient satisfies a = 0 × q + r for a non-zero a. Python raises ZeroDivisionError, Java throws ArithmeticException on integers, JavaScript returns NaN, and C leaves the behaviour undefined. Guard the modulus before the operation rather than catching the failure after it.

Why is a number mod 1 always 0?

Every whole number mod 1 equals 0, since 1 divides every whole number exactly. 7 = 1 × 7 + 0 and 250 = 1 × 250 + 0, so nothing is left over. The remainder has to fall in the range 0 to 0 when the modulus is 1, which leaves a single possible answer.

What happens when the modulus is greater than the dividend?

The result equals the dividend, when the modulus is greater than it. 1 mod 2 = 1 and 5 mod 10 = 5, since 2 fits into 1 zero times and 10 fits into 5 zero times, leaving the whole dividend behind. This holds for every positive dividend smaller than the modulus.

What does a modulo result of 0 mean?

A modulo result of 0 means the division is exact and the dividend is a multiple of the modulus. 496 mod 4 = 0 confirms 496 is a multiple of 4, while 226 mod 4 = 2 shows 226 is not. Testing x % n === 0 is the standard divisibility check in code.