Skip to content

Modulo Calculator

Enter a number and a modulus to see the remainder under all three conventions — truncated, floored and Euclidean — side by side, so a negative number never surprises you. Switch modes to add, subtract, multiply and raise to powers mod n, or to print a mod n table. Free, no sign-up, and decimals are exact.

What do you want to do?

Negative numbers and decimals are fine.

The number you divide by. It can be negative or a decimal, but not zero.

Results

2

−7 mod 3

−3

Quotient, q

No

Is a divisible by n?

The three conventions side by side

ConventionQuotient qRemainder rCheck: q × n + r = aUsed by
Euclidean−32(−3) × 3 + 2 = −7Mathematics texts for n > 0; Rust rem_euclid
Floored−32(−3) × 3 + 2 = −7Python and Ruby %, Excel MOD, Java Math.floorMod, Kotlin mod
Truncated−2−1(−2) × 3 + (−1) = −7C, C++, Java, JavaScript, Go, PHP, Swift and Rust %

The conventions differ because a or n is negative. Truncated rounds the quotient toward zero, floored rounds it down, and Euclidean keeps the remainder at 0 or above and below |n|.

How to Use the Modulo Calculator

  1. 1. Remainder mode. Enter a and n. The calculator divides, shows the quotient and the remainder under each convention, and checks that quotient × n + remainder gives back a. Pick which convention to show as the headline.
  2. 2. Modular arithmetic mode. Enter two whole numbers and a modulus, then choose addition, subtraction, multiplication or power. It reduces the result mod n and, for powers, shows the square-and-multiply table that keeps the numbers small.
  3. 3. Table mode. Choose a modulus from 2 to 16 for an addition or multiplication table. The cells equal to 0 (addition) or 1 (multiplication) are highlighted, which pairs each number with its inverse.
  4. 4. Mind the signs. For positive a and n every convention agrees. They split when a or n is negative, and the table below shows which programming languages use which.

What “a mod n” Means

Dividing a by n gives a quotient q and a remainder r with a = q × n + r. The remainder is what is left over after taking out as many whole copies of n as fit. On a 12-hour clock, 17 hours after midnight reads 5 because 17 = 1 × 12 + 5, so 17 mod 12 = 5. If it is Tuesday, 100 days later is Thursday, since 100 mod 7 = 2. Two numbers are congruent mod n when they leave the same remainder; 17 ≡ 5 (mod 12).

Negative Numbers: Three Conventions

When a or n is negative, the quotient can be rounded in different directions, and each choice defines a different remainder. All three satisfy a = q × n + r with |r| < |n|.

  • Truncated rounds the quotient toward zero, so the remainder takes the sign of a. −7 divided by 3 is −2.33…, which truncates to −2, leaving r = −7 − (−2 × 3) = −1.
  • Floored rounds the quotient down, toward negative infinity, so the remainder takes the sign of n. The quotient becomes −3 and r = −7 − (−3 × 3) = 2.
  • Euclidean insists that 0 ≤ r < |n| whatever the signs. For −7 and 3 it gives 2, like the floored version; for 7 and −3 it gives 1, where floored gives −2.
−7 mod 3 in common languages
WhereConventionResult
−7 % 3 in C, C++, Java, JavaScript, Go, Rust, PHP, SwiftTruncated−1
−7 % 3 in Python and RubyFloored2
=MOD(−7, 3) in ExcelFloored2
Math.floorMod(−7, 3) in JavaFloored2
(−7).rem_euclid(3) in RustEuclidean2

The JavaScript trap

In JavaScript, −1 % 5 is −1, not 4, so using % to wrap an array index around goes out of range. The usual fix is ((a % n) + n) % n, which gives the floored (and, for positive n, Euclidean) result. Python needs no fix, because its % is already floored.

Modular Arithmetic Worked Example

Sums, differences and products can be reduced mod n at any stage, which is what keeps the numbers small. The calculator opens in this mode on 123 × 45 mod 7:

  • Directly: 123 × 45 = 5535, and 5535 = 790 × 7 + 5, so the result is 5.
  • Reduced first: 123 ≡ 4 and 45 ≡ 3 (mod 7), so 123 × 45 ≡ 4 × 3 = 12 ≡ 5 (mod 7). Same answer, much smaller numbers.
  • Powers: 3²⁰⁰ mod 13 = 9. Squaring 3 repeatedly mod 13 (3, 9, 3, 9, …) and multiplying the squares that match the binary digits of 200 = 128 + 64 + 8 gives it without ever forming 3²⁰⁰.

Division is the exception. You can cancel a common factor only when it has no factor in common with n, and “dividing” by a means multiplying by its modular inverse, which exists exactly when a and n are coprime. The Modular Inverse Calculator finds it.

Reading a Mod n Table

In the multiplication table mod 5, every non-zero row contains a 1 — each of 1, 2, 3 and 4 has an inverse (2 × 3 = 6 ≡ 1; 4 × 4 = 16 ≡ 1). Mod 6 is different: 2 × 3 = 6 ≡ 0 although neither factor is 0, and the rows for 2, 3 and 4 contain no 1, because those numbers share a factor with 6. Prime moduli give tables where every non-zero number has an inverse.

Frequently Asked Questions

What is −7 mod 3?
In mathematics and in Python it is 2, because −7 = (−3) × 3 + 2. In C, Java and JavaScript, the % operator gives −1, because the quotient is truncated to −2. Both are correct; they are different conventions. The calculator shows each.
Is the % operator the same as modulo?
Not always. In most languages, % is a remainder operator whose sign follows the dividend, so it differs from the mathematical modulo for negative numbers. Python and Ruby define % as floored modulo instead.
What is the difference between modulo and remainder?
For positive numbers there is none. For negative ones, “remainder” usually means the truncated result (sign of the dividend) and “modulo” the floored or Euclidean one, but the terms are used loosely, so check how your language or textbook defines them.
What is a mod 0?
It is undefined, as division by zero is, and the calculator reports an error rather than a number.
How do you 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 correspond to 1 digits in the binary form of the exponent. The mod n power mode does this and shows the table, even for exponents with dozens of digits.
Can I take the modulo of a decimal?
Yes. 5.5 mod 2 = 1.5, and 0.3 mod 0.1 = 0 exactly, which a floating-point % reports as 0.0999…. The calculator scales decimals to whole numbers first, so there is no rounding error.

Related Tools

More in Math Calculators

See all Math Calculators