Education & Research Tools

Modulo Calculator

Calculate a mod n with the quotient and remainder, the difference between the truncated and floored conventions for negative numbers, modular exponentiation for very large values, and the modular inverse.

  • Remainder and quotient
  • Language conventions compared
  • Modular power and inverse
Runs in your browser

Everything you paste, type or drop is processed in this browser tab. It is not uploaded, logged, stored or sent to analytics.

Modulo workspace

1 a mod n

Try it:

Whole numbers of any size up to 2,000 digits, and exact decimals such as 5.5. Spaces, commas and underscores between digits are ignored.

2 Power and inverse (optional)

Uses a as the base and n as the modulus. Negative exponents use the inverse.

Modular inverse

3 The remainder

Enter a number and a divisor.

What the Modulo Calculator does

This modulo calculator gives the remainder of a divided by n - a mod n - together with the quotient, exactly, for numbers of any size up to 2,000 digits and for decimals such as 5.5. It shows all three conventions side by side, because -17 mod 5 is 3 in mathematics, Python and Excel but -2 in C, Java and JavaScript, and it also does modular exponentiation (a^e mod n) and the modular inverse with the working shown.

All arithmetic uses exact big integers in your browser, so there is no floating-point rounding: 0.3 mod 0.1 is 0, not 0.09999. Nothing you type is sent anywhere.

How to use it

  1. Enter the number a and the divisor n. Negative numbers, decimals and very long numbers are all fine; spaces or commas between digits are ignored.
  2. Choose how you want the headline answer: the mathematical remainder (never negative), the floored remainder used by Python and Excel, or the truncated remainder of C, Java and JavaScript's %. All three are shown underneath either way.
  3. Read the equation a = q x n + r to see the quotient and remainder fit together, and the language table to see what your programming language would return.
  4. For modular exponentiation, type an exponent: the tool computes a^e mod n by square-and-multiply and lists the steps when the exponent is small enough.
  5. Leave "Also find the inverse" ticked to get a^-1 mod n with the extended Euclidean algorithm, or a clear statement that none exists because gcd(a, n) is not 1.

Reading the results

Every convention satisfies a = q x n + r with |r| smaller than |n|. They differ only in which way the quotient is rounded: towards zero (truncated), downwards (floored) or so that r is never negative (Euclidean). For positive a and n they are identical.

If you are writing code, pick the row for your language. Bugs from this difference are common: using % to wrap an array index with a negative offset in JavaScript or Java gives a negative index, where Python would wrap correctly.

In modular arithmetic proper - clock arithmetic, residues, cryptography - the Euclidean remainder is the one meant: -17 is congruent to 3 modulo 5.

A modular inverse x satisfies a x x = 1 (mod n). It exists only when a and n share no common factor; the working table shows each step of the extended Euclidean algorithm, where every row keeps r = s x a + t x n.

Worked example: -17 mod 5, and a small RSA calculation

17 divided by 5 is 3 remainder 2, because 3 x 5 + 2 = 17. For -17 the conventions split. Truncated division rounds -3.4 towards zero to -3, so r = -17 - (-3 x 5) = -2. Floored division rounds down to -4, so r = -17 - (-4 x 5) = 3. The Euclidean remainder must be non-negative, which also gives 3.

So -17 % 5 is -2 in JavaScript, Java and C, and -17 % 5 is 3 in Python, as is =MOD(-17,5) in Excel. With a negative divisor, 17 mod -5 is 2 truncated but -3 floored, because the floored remainder takes the sign of the divisor.

For RSA with p = 61 and q = 53, n = 3233 and the public exponent is 17. Encrypting the message 65 is 65^17 mod 3233 = 2790. The private exponent is the inverse of 17 modulo (61 - 1) x (53 - 1) = 3120, which the extended Euclidean algorithm finds as 2753 because 17 x 2753 = 46,801 = 15 x 3120 + 1.

Decrypting, 2790^2753 mod 3233 = 65 again. The calculator never writes out 2790^2753 - a number with over 9,000 digits - because square-and-multiply reduces modulo 3233 after every step.

Formulas and scoring rules

Division identity
a = q x n + r, |r| < |n|
Truncated
q = trunc(a / n); r = a - n x q (r has the sign of a)
Floored
q = floor(a / n); r = a - n x q (r has the sign of n)
Euclidean
r = a - |n| x floor(a / |n|); 0 <= r < |n|
Modular exponentiation
a^e mod n by square-and-multiply: for each bit of e, result = result x a^(2^i) mod n when the bit is 1A negative e is computed as (a^-1)^|e| mod n.
Modular inverse
extended Euclid: r_(i+1) = r_(i-1) - q_i r_i, likewise s and t; inverse = s mod n when gcd = 1Decimals are handled by scaling both numbers to whole numbers first, so results are exact; nothing is rounded.

Limitations: what the result does not prove

  • Numbers are limited to 2,000 digits each so the page stays responsive. That is far beyond everyday needs and enough for textbook cryptography, but real cryptographic code should use a vetted library, not a web page.
  • Modular exponentiation and the inverse need whole numbers; decimals are accepted only for the plain remainder.
  • The language table describes the built-in operators and functions as they are commonly documented. Libraries and newer language versions may add other functions (for example Euclidean variants), so check your language's documentation for edge cases.
  • Scientific notation such as 1e6 is not accepted, to avoid ambiguity; type the digits.

Privacy: where your data goes

Everything you paste, type or drop is processed in this browser tab. It is not uploaded, logged, stored or sent to analytics. Session recording and tag-manager scripts are switched off on this page.

Standards and sources

Frequently asked questions

How do you calculate a mod n?

Divide a by n, keep the whole-number part of the quotient, and subtract that many n's from a. For 47 mod 6: 47 / 6 = 7.83, so q = 7, and 47 - 7 x 6 = 5. The remainder 5 is the answer, and 47 = 7 x 6 + 5 checks it.

What is the mod of a negative number?

It depends on the convention. In mathematics, Python and Excel, -17 mod 5 is 3, because -17 = -4 x 5 + 3. In C, C++, Java and JavaScript, -17 % 5 is -2, because those languages round the quotient towards zero. Both satisfy a = q x n + r; this page shows both side by side.

Why does JavaScript give a negative remainder?

Because % in JavaScript is a remainder operator that truncates the quotient towards zero, so the result takes the sign of the left-hand number. To get the mathematical result for a positive n, use ((a % n) + n) % n, or BigInt arithmetic for large numbers.

How do I calculate modulo of very large numbers?

Use exact integer arithmetic rather than a normal calculator, which rounds after about 15 to 17 significant digits. Type or paste the full number here - up to 2,000 digits - and the remainder is computed exactly with big integers in your browser.

How do I calculate modular exponentiation like 7^560 mod 561?

Use square-and-multiply: write the exponent in binary, repeatedly square the base modulo n, and multiply in the squares where the bit is 1, reducing modulo n each time. Enter 7 as a, 561 as n and 560 as the exponent; the answer is 1, which is why 561 is a famous Carmichael number.

How do I find the modular inverse?

Run the extended Euclidean algorithm on a and n. If the greatest common divisor is 1, the coefficient of a in the final row is the inverse, adjusted to lie between 0 and n - 1. For example, 3 x 4 = 12 = 1 (mod 11), so the inverse of 3 mod 11 is 4. If the gcd is not 1, no inverse exists.

Can you take the modulo of a decimal number?

Yes. The same identity a = q x n + r applies with a whole-number quotient: 5.5 mod 2 = 1.5, since 5.5 = 2 x 2 + 1.5. This calculator does it exactly, so 0.3 mod 0.1 gives 0, where floating-point arithmetic in many languages gives 0.09999999999999998.

Last reviewed by the A2Z.Tools team against the sources listed above.

Rate this tool

Was this tool useful? Your feedback helps us improve it.

No ratings yet — be the first to rate this tool.
Your rating (required)
0 / 2000

Please do not include passwords, payment details or other sensitive information.

Your feedback is sent privately to the A2Z.Tools team and will not be posted publicly.

Advertisement