Everyday utilities

Modulo Calculator

Modulo Calculator

a mod n with floored, truncated and Euclidean conventions, explained for negative numbers.

About this tool

About Modulo Calculator

The modulo operation gives the remainder after division: 17 mod 5 = 2 because 17 = 5 × 3 + 2. With positive numbers that is the whole story, but with a negative dividend or divisor, programming languages and textbooks disagree, which is a common source of bugs. This calculator shows all three conventions at once. Floored modulo, used by Python, Ruby and Excel's MOD function, takes the sign of the divisor, so −17 mod 5 = 3. Truncated remainder, used by the % operator in C, C++, Java and JavaScript, takes the sign of the dividend, so −17 % 5 = −2. Euclidean modulo is always zero or positive. The floored and truncated results show their quotients, and a check line confirms the floored answer. Whole numbers of any length use exact big-integer arithmetic, so a 30-digit dividend is no problem, and decimals work too. It runs in your browser.

Built and maintained by Abhi JainUpdated

How to Calculate a Modulo

  1. 1

    Enter a

    Type the number being divided into Dividend (a). Negative numbers and decimals are fine.

  2. 2

    Enter n

    Type the divisor into Divisor (n).

  3. 3

    Compare conventions

    Read the floored, truncated and Euclidean results and use the one your language or textbook expects.

Modulo Calculator: questions and answers

What does mod mean?

a mod n is the remainder r in a = n × q + r, where q is a whole-number quotient. 23 mod 7 = 2 because 23 = 7 × 3 + 2.

What is −17 mod 5?

It depends on the convention. With floored division (Python, Excel) it is 3, since −17 = 5 × (−4) + 3. With truncated division (C, Java, JavaScript) it is −2, since −17 = 5 × (−3) − 2.

How do I get a positive result in JavaScript?

Use ((a % n) + n) % n, which gives the floored result for a positive n.

Is modulo the same as remainder?

For positive numbers, yes. For negative numbers the remainder (truncated) and the modulo (floored or Euclidean) can differ by the divisor.

Where is modulo used?

Clock arithmetic (14:00 is 2 o'clock because 14 mod 12 = 2), checking even or odd numbers, cycling through lists, check digits and cryptography. The GCD and LCM Calculator uses repeated remainders in Euclid's algorithm.