Skip to main content
CalcChef

Modulo Calculator

a mod n for any two numbers: the remainder when both are positive (17 mod 5 = 2), and the sign of the divisor for negatives (-7 mod 3 = 2). How it works

Use the Modulo Calculator

Result (a mod n)
1
Quotient 3
10 mod 3 = 1
10 = 3 × 3 + 1

About the Modulo Calculator

a mod n = a − n × ⌊a ÷ n⌋, so the result has the sign of the divisor: 10 mod 3 = 1, −7 mod 3 = 2 and 10 mod −3 = −2. For two positive numbers that is the ordinary remainder; for a negative dividend it is not the remainder long division gives (−7 ÷ 3 is −2 remainder −1). Decimals are worked out exactly, so 0.3 mod 0.1 = 0, and a number such as 9007199254740993 keeps every digit: 9007199254740993 mod 10 = 3. The quotient is shown too (−7 = −3 × 3 + 2), and the operation menu adds (a + b) mod n, (a × b) mod n and powers: 4^13 mod 497 = 445.

This calculator and the % operator in JavaScript

ana mod n herea % n in JavaScript
10311
-732-1
7-3-21
-7-3-1-1
10-3-21
-132-1
-5127-5
5.521.51.5
-5.520.5-1.5
0.30.100.09999999999999998
90071992547409931032
1234567890123456789971086

The two agree when a and n are positive. JavaScript’s % takes the sign of the dividend and works on binary doubles, which is why the last two rows and 0.3 % 0.1 differ.

Clocks, angles and time as modulo questions

QuestionCalculationResult
13:00 on a 12-hour clock13 mod 121
25 hours after 12 o’clock25 mod 121
5 hours before 12 o’clock-5 mod 127
An angle of 725 degrees725 mod 3605
A compass heading of -30 degrees-30 mod 360330
Seconds left over in 1,000,000 seconds1000000 mod 6040
Odd or even: -3-3 mod 21

A result of 0 for n = 2 means even and 1 means odd, for negative numbers too.

How to use this tool

  1. Pick the operation: a mod n, (a + b) mod n, (a × b) mod n or aᵇ mod n.
  2. Enter the numbers. a and n can be negative or have decimals; a power needs whole numbers and an exponent of 0 or more.
  3. Read the result, the quotient and the line a = q × n + r underneath it.
  4. A zero divisor, an empty field or text that is not a number shows a message next to the field and no result.

Frequently asked questions

What is -7 mod 3?
2 here. The calculator uses floored modulo, a − n × ⌊a ÷ n⌋, whose result has the sign of the divisor, as Python’s % does. JavaScript’s % takes the sign of the dividend and gives -1 for -7 % 3; the MDN page calls it a remainder operator and gives ((n % d) + d) % d for a true modulo.
How do I calculate modulo by hand?
Divide, round down, multiply back and subtract. 17 mod 5: 17 ÷ 5 = 3.4, which rounds down to 3, and 17 − 5 × 3 = 2. For a negative dividend, round down means towards minus infinity: -13 ÷ 5 = -2.6 rounds to -3, and -13 − 5 × (-3) = 2.
How do I check whether a number is even or odd?
Take it mod 2: 0 means even and 1 means odd. 10 mod 2 = 0, 17 mod 2 = 1, and -3 mod 2 = 1, so negative numbers work because the result takes the sign of the divisor 2.
Why is 0.3 mod 0.1 equal to 0 here but 0.09999999999999998 in code?
The two numbers are read as exact decimals here: 0.3 is 3 tenths and 0.1 is 1 tenth, so 3 tenths divide by 1 tenth exactly with nothing left. JavaScript stores them as binary fractions that are slightly off, and its 0.3 % 0.1 returns 0.09999999999999998.
What happens when the divisor is 0 or the numbers are huge?
A zero divisor shows The divisor cannot be zero. and no result. A value outside about 1e-300 to 1e300 is refused as outside the range the tool reads, and a form like 1e3 is read as 1000. The share link carries both fields (?a=17&b=5); a link value such as 1,000 is shown with a message instead of a result. A huge power is fine: aᵇ mod n works by repeated squaring, so 2^(10^50) mod 7 = 2 comes back at once, with an exponent of up to 200 digits.
How is modulo used for clocks and angles?
It wraps a number into a fixed range. 13 mod 12 = 1 turns 13:00 into 1 o’clock, -5 mod 12 = 7 is 5 hours before 12 o’clock, 725 mod 360 = 5 reduces an angle of 725 degrees, and -30 mod 360 = 330 turns a heading of -30 degrees into 330.

Related Tools

View all tools