Modulo calculator (a mod n)

Enter the dividend and divisor to get the remainder. The tool shows how the math definition differs from some programming languages for negative numbers.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter dividend a and divisor n.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

a mod n = a - n × ⌊a ÷ n⌋ (the result takes the sign of n). Truncated remainder = a - n × trunc(a ÷ n) (sign of a, like % in JavaScript and C).

Example

Inputs: Dividend a = 17, Divisor n = 5. Result: a mod n: 2; Integer quotient ⌊a ÷ n⌋: 3; Truncated remainder (a % n in code): 2.

Limits and caveats

  • Uses floored division, so the result takes the sign of the divisor.
  • Decimal inputs use standard floating point precision.

Questions about this tool

What is 17 mod 5?

2, because 17 = 5 × 3 + 2.

Why is -17 mod 5 equal to 3 and not -2?

In math the result lies between 0 and n, and -17 = 5 × (-4) + 3. JavaScript and C return -2 because they truncate the quotient.

Where is modulo used?

In weekday and clock arithmetic, check digits, cryptography and round-robin scheduling.