Skip to content

Modulo Calculator (Remainder, Mod Power and Inverse)

Find a mod n and the quotient and remainder under truncated, floored and Euclidean rules, for huge integers or decimals, plus modular powers and inverses.

Calculator Details

17 mod 5:2See results ↓

a mod n with quotient and remainder, a^b mod n, or the modular inverse of a

A whole number of any length, or a decimal for a mod n

Not zero; a whole number for powers and inverses

Your Results

17 mod 5: 2

17 mod 5

2

Quotient 3

Truncated (JavaScript, C %)

2

Quotient 3

Floored (Python, Excel MOD)

2

Quotient 3

Euclidean (always ≥ 0)

2

Quotient 3

17 mod 5 = 2: 17 = 3 × 5 + 2.

Compare scenarios

Save these inputs as a scenario, change them, and compare the results side by side.

Loading, available in a moment

Detailed Breakdown

17 ÷ 5 under each convention
ConventionQuotient qRemainder rUsed by
Truncated32JavaScript, C, C++, Java, C# % · remainder has the sign of a
Floored32Python %, Excel MOD, Google Sheets · remainder has the sign of n
Euclidean32Number theory · remainder always 0 ≤ r < |n|

Each row satisfies a = q × n + r. They agree whenever a and n are both positive.

How This Calculator Works

How It’s Calculated

Division with remainder finds q and r with a = q × n + r. The three conventions differ only in how they round the quotient when a or n is negative.

Truncated: q = trunc(17 ÷ 5) = 3, r = 17 − 3 × 5 = 2

Floored: q = ⌊17 ÷ 5⌋ = 3, r = 2

Euclidean: r = 2 with q = 3

Frequently Asked Questions

What does mod mean?

a mod n is the remainder when a is divided by n. 17 mod 5 = 2 because 17 = 3 × 5 + 2. Clocks are the everyday example: 15:00 is 3 o’clock because 15 mod 12 = 3.

What is −7 mod 3?

It depends on the convention. Python and Excel’s MOD give 2 (the remainder takes the divisor’s sign); JavaScript, C and Java’s % operator give -1 (the remainder takes the dividend’s sign); mathematicians usually use the Euclidean remainder, 2, which is never negative. The calculator shows all three.

Is modulo the same as remainder?

For positive numbers, yes. With negative numbers, “remainder” often means the truncated result (JavaScript calls % the remainder operator) while “modulo” means the floored or Euclidean result. Check which one your language or spreadsheet uses before relying on it.

Can I take the modulo of a decimal?

Yes. 5.5 mod 2 = 1.5 because 5.5 = 2 × 2 + 1.5. The calculator works with the exact decimal you type, so it avoids the floating-point surprises of some languages (where 0.3 % 0.1 is not exactly 0).

How do I calculate a large power mod n?

Use square-and-multiply: square the base repeatedly, reducing mod n each time, and multiply together the squares that match the 1 bits of the exponent. 4¹³ mod 497 = 445 needs only a handful of small multiplications instead of computing 4¹³ = 67,108,864 first. This is how RSA and Diffie–Hellman are computed.

What is a modular inverse?

The number x with a × x ≡ 1 (mod n). The inverse of 3 mod 11 is 4, because 3 × 4 = 12 = 11 + 1. It exists only when a and n share no common factor (gcd = 1); 6 has no inverse mod 9, for example.

How big can the numbers be?

Whole numbers of up to 400 digits each, calculated exactly with arbitrary-precision integers. Exponents for modular powers can have up to 300 digits; the work grows only with the number of digits, not with the size of the exponent.

Included: quotient and remainder under the truncated (ECMA-262 %), floored (Python, Excel MOD) and Euclidean conventions for whole numbers of any length and exact decimals; modular powers by square-and-multiply and modular inverses by the extended Euclidean algorithm, with their steps. Left out: long-division working digit by digit, the Chinese remainder theorem, modular square roots and discrete logarithms. The site’s scientific calculator uses the floored convention (−7 mod 3 = 2).

Modulo Calculator: Remainders, Negative Numbers and Mod Powers

By FindMySum · Last reviewed

Modulo is the remainder after division: 17 mod 5 = 2 because 5 goes into 17 three times with 2 left over. It is one of the most used operations in programming (even and odd tests, wrapping around arrays, hashing, clock arithmetic, check digits) and the foundation of number theory and cryptography. This calculator does three jobs: division with remainder under every common convention, modular powers, and modular inverses.

Division with remainder

For whole numbers a (the dividend) and n (the divisor, not zero) there is a quotient q and a remainder r with

a = q × n + r

The remainder is smaller than the divisor in size. When a and n are positive there is only one such pair, and every calculator and language agrees: 17 = 3 × 5 + 2, so the quotient is 3 and 17 mod 5 = 2. That is the calculator's default example.

Negative numbers: three conventions

When a or n is negative, there are two valid remainders, one positive and one negative, and different tools pick different ones. The difference comes from how they round the quotient:

ConventionQuotientRemainder sign−7 mod 37 mod −3Used by
Truncatedrounded toward 0same as a−11JavaScript, C, C++, Java, C#, Go, Rust
Flooredrounded downsame as n2−2Python, Ruby, Excel MOD, Google Sheets
Euclideanchosen so r ≥ 0never negative21Number theory textbooks

All three satisfy a = q × n + r. For −7 and 3, truncation gives q = −2, so r = −7 − (−6) = −1; flooring gives q = −3, so r = −7 − (−9) = 2.

The calculator shows all three side by side, with each quotient, and headlines the Euclidean remainder because it is the one mathematics means by "a mod n". If you are checking code, read the row for your language.

Decimals

The floored and truncated definitions work for any real numbers: 5.5 mod 2 = 1.5 and −5.5 mod 2 = 0.5 (floored) or −1.5 (truncated). Programming languages store decimals in binary floating point, which is why 0.3 % 0.1 in JavaScript gives 0.09999999999999998 instead of 0. The calculator works in exact decimal arithmetic, so typed decimals give exact answers.

Very large integers

Whole numbers are handled with arbitrary-precision integers, so a 30-digit dividend such as 123456789012345678901234567890 mod 9876543210 gives the exact remainder 1562499990, where a spreadsheet would lose the last digits. Inputs can have up to 400 digits.

Modular exponentiation

Computing a^b mod n by working out a^b first is hopeless for the numbers used in cryptography, where a^b would have more digits than there are atoms in the universe. Square-and-multiply avoids it:

  1. Write the exponent b in binary.
  2. Start with result = 1 and square = a mod n.
  3. For each bit from the right: if the bit is 1, multiply result by square (mod n); then square the square (mod n).

For 4^13 mod 497, 13 is 1101 in binary. The squares are 4, 16, 256 and 256² mod 497 = 429. Multiplying the ones for bits 0, 2 and 3 gives 4 × 256 × 429 mod 497 = 445. The breakdown table lists every step. The number of steps grows with the number of digits in b, not with b itself, which is why a 300-digit exponent is no problem.

Modular inverse

The inverse of a modulo n is the number x for which a × x leaves remainder 1: a × x ≡ 1 (mod n). It plays the role of division in modular arithmetic. The inverse exists exactly when a and n have no common factor other than 1.

The extended Euclidean algorithm finds it. Euclid's algorithm repeatedly replaces (a, n) by (n, a mod n) until the remainder is 0; the extended version keeps track of how each remainder is built from a and n, so when the remainder reaches 1 it has found s and t with s × a + t × n = 1. Then s mod n is the inverse.

The classic RSA teaching example uses n = 3120 (that is, (61 − 1) × (53 − 1)) and the public exponent 17. The private exponent is the inverse of 17 mod 3120, which the calculator finds as 2753: 17 × 2753 = 46,801 = 15 × 3120 + 1.

Where modulo shows up

  • Even and odd: n mod 2 is 0 for even numbers.
  • Clock and calendar arithmetic: 30 hours after 22:00 is (22 + 30) mod 24 = 4:00; the day of the week is a mod 7 calculation.
  • Wrapping indexes: moving through a list of length L, the next position is (i + 1) mod L.
  • Check digits: ISBN-10, credit card Luhn checks and Canadian social insurance numbers all use remainders.
  • Hashing: a hash table puts a key in bucket hash(key) mod size.
  • Cryptography: RSA, Diffie–Hellman and elliptic-curve systems are all modular arithmetic on very large numbers.

Common mistakes

  • Assuming % in your language returns a non-negative result for negative numbers.
  • Testing n % 2 == 1 to find odd numbers in a truncating language.
  • Using floating point for money or large integers before taking a remainder.
  • Expecting a modular inverse when a and n share a factor.

More questions

What is 0 mod n?

0, for any non-zero n, because 0 = 0 × n + 0. Zero divides evenly by everything, so every convention agrees.

What is n mod 0?

Undefined, like division by zero. Some languages throw an error, JavaScript returns NaN, and Excel shows #DIV/0!. The calculator asks for a non-zero divisor.

Why does Excel's MOD give a different answer from my code?

Excel's MOD uses the floored convention, so the result has the sign of the divisor: MOD(−7, 3) = 2. JavaScript, C, C++, Java and C# use truncation, so −7 % 3 = −1. Python matches Excel. When you port a formula between the two, negative inputs are where it breaks.

How do I check if a number is even with modulo?

A number is even when n mod 2 = 0. In truncating languages, test for 0 (or for not 0) rather than for 1, because −3 % 2 is −1, not 1, and a test for 1 silently treats negative odd numbers as even.

What does congruent mod n mean?

Two numbers are congruent mod n when they leave the same Euclidean remainder, or equivalently when their difference is a multiple of n. 17 ≡ 2 (mod 5) and −3 ≡ 2 (mod 5) because 17 − 2 = 15 and −3 − 2 = −5 are both multiples of 5.

Sources

This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.

Spotted a wrong result or a bug? Report a problem

May we count your visits with Invoker Analytics? It keeps a random visitor number in your browser. Your calculator inputs are never sent. Privacy policy