Skip to content
MoneyDeck

Modulo Calculator

Compute a mod n and the remainder, with negative and decimal numbers

Updated · Free, no signup

a mod n (floored, Python/Excel)

2

Floored quotient ⌊a ÷ n⌋

3

Truncated remainder (C/Java/JavaScript %)

2

Euclidean remainder (always ≥ 0)

2

Exact quotient a ÷ n

3.4

Check

17 = 5 × 3 + 2

About the Modulo Calculator

This modulo calculator finds a mod n — the remainder left over when a is divided by n — together with the integer quotient. It works with negative numbers and decimals, and shows the answer under each of the conventions you are likely to meet, because “mod” does not give the same result everywhere once negatives are involved.

Programmers use it to check what % does in their language, students use it for modular arithmetic and number theory homework, and it is handy for everyday problems like working out what weekday it will be in 100 days (100 mod 7) or converting minutes into hours and minutes.

The main result uses the floored (mathematical) definition, where the remainder has the same sign as the divisor — this is what Python, Excel’s MOD function and most maths textbooks use. The truncated remainder (C, Java, JavaScript %) and the always-non-negative Euclidean remainder are shown as well.

With the default inputs, the a mod n (floored, python/excel) is 2. Change any value above to recalculate instantly.

How to use the modulo calculator

  1. 1Enter the dividend a (the number you are dividing).
  2. 2Enter the modulus n (what you are dividing by).
  3. 3Read a mod n and the floored quotient.
  4. 4If either number is negative, compare the truncated and Euclidean results to match your programming language.

Formula and method

a mod n = a − n × ⌊a ÷ n⌋

The floored modulo subtracts the largest whole multiple of n that does not exceed a (for positive n). The quotient is rounded down toward negative infinity, so the remainder always takes the sign of the divisor and lies between 0 and n.

The truncated remainder instead rounds the quotient toward zero (a − n × trunc(a ÷ n)), so its sign follows the dividend; this is how the % operator works in C, C++, Java and JavaScript. The Euclidean remainder is always between 0 and |n| − 1 regardless of signs. For two positive numbers all three agree.

a
Dividend — the number being divided
n
Divisor or modulus
⌊x⌋
Floor: the largest integer not greater than x

Worked examples

17 mod 5

5 goes into 17 three whole times (15), leaving 17 − 15 = 2. With two positive numbers every convention gives the same remainder of 2.

Negative dividend: −17 mod 5

Floored division gives ⌊−3.4⌋ = −4, and −17 − 5 × (−4) = 3. JavaScript’s −17 % 5 truncates the quotient to −3 instead and returns −2.

Negative divisor: 17 mod −5

With a negative modulus the floored result takes the divisor’s sign: 17 − (−5)(−4) = −3. The truncated and Euclidean remainders are both 2.

Which weekday in 100 days?

100 days is 14 full weeks plus 2 days, so 100 mod 7 = 2. If today is Monday, 100 days from now is a Wednesday.

Decimal: 7.5 mod 2

Three whole 2s fit into 7.5 (6), leaving 1.5.

Frequently asked questions

What does mod mean in math?+

a mod n is the remainder after dividing a by n. Two numbers are “congruent mod n” when they leave the same remainder, which is the basis of clock arithmetic: 15:00 is 3 o’clock because 15 mod 12 = 3.

What is −1 mod 5?+

Under the mathematical (floored or Euclidean) definition, −1 mod 5 = 4, because −1 = 5 × (−1) + 4. In C, Java and JavaScript, −1 % 5 returns −1 because those languages truncate the quotient toward zero.

Why does JavaScript give a negative remainder?+

JavaScript’s % is a remainder operator, not a true modulo: its result takes the sign of the dividend. To get a non-negative result for positive n use ((a % n) + n) % n.

What is the difference between modulo and remainder?+

For positive numbers they are identical. With negatives, “remainder” usually means the truncated version (sign follows the dividend) while “modulo” means the floored or Euclidean version (result in the range 0 to n − 1 for positive n).

What is a mod 0?+

It is undefined, because division by zero is undefined. Some languages return NaN or throw an error; this calculator shows the result as undefined.

Related tools