Skip to content

Modulo Calculator

Calculate the remainder of division (modulo operation) for any two numbers.

Modulo Calculator tool

Loading...

Example

Input: 17 mod 5 (dividend 17, divisor 5)

Result: 2 (quotient 3; 17 = 5 x 3 + 2)

17 / 5 = 3.4, floored to 3; 17 - 5 x 3 = 2, so the remainder is 2.

Floored Modulo vs the % Operator

ExpressionQuotient (floored)Modulo (Python, this tool)% in C, Java, JS
17 mod 5322
-17 mod 5-43-2
17 mod -5-4-32
-17 mod -53-2-2
-1 mod 12-111-1
5.5 mod 221.51.5

The two results differ only when the signs differ and the division is not exact.

  1. Enter the dividend (the number being divided) in the first field.
  2. Enter the divisor (the number to divide by) in the second field.
  3. View the remainder (modulo result) and quotient instantly.
  4. See the complete division equation showing dividend = divisor x quotient + remainder.
  5. With negative numbers, compare the floored modulo with the truncated remainder that C, Java, and JavaScript return.
  6. Click the copy button to copy the result.

Remainders, Negative Numbers and Modulo

The modulo operation gives the remainder after division. 17 mod 5 = 2, because 5 goes into 17 three times with 2 left over. This calculator shows the remainder, the whole-number quotient, and a check line such as 17 = 5 x 3 + 2.

With positive numbers, everyone agrees on the answer. With negative numbers there are two conventions, and they give different results. This calculator uses floored modulo, the one used in math and in Python. Its answer always has the same sign as the divisor, so -17 mod 5 = 3. The % operator in C, Java and JavaScript works differently. It gives a truncated remainder that takes the sign of the dividend, so it returns -2. When the two differ, the calculator shows both, which helps when porting code between languages.

Modulo is behind many everyday patterns. A number is even when n mod 2 is 0. Clock time wraps around: 5 hours after 10 o'clock is (10 + 5) mod 12 = 3 o'clock. Days of the week work the same way. 100 days after a Monday is 100 mod 7 = 2 days later in the week, a Wednesday. Programmers use it to wrap list positions and spread items across hash tables. Bank account checks such as the IBAN Validator use mod 97.

Decimals work too, so 5.5 mod 2 is 1.5. The tool cleans up tiny floating point errors so results such as 0.3 mod 0.1 come out as 0. A divisor of 0 is not allowed. For the largest number that divides two values, see the GCD and LCM Calculator.

Formula / How It Works

a mod b = a - b x floor(a / b) | Division algorithm: a = b x q + r where q = floor(a / b), |r| < |b|, and r has the same sign as b (or is 0)

Frequently Asked Questions

Modulo finds the remainder after dividing one number by another. For example, 17 mod 5 = 2, because 5 x 3 = 15 and 17 - 15 = 2. It is written as a mod b in math and often as a % b in code. The result is always smaller in size than the divisor.
It depends on the convention. With floored modulo, used by this calculator, Python and most math texts, -7 mod 3 = 2, because -7 = 3 x (-3) + 2. With the truncated remainder from the % operator in C, Java and JavaScript, the answer is -1. The calculator shows both when they differ.
It checks if a number is even (n % 2 == 0), wraps an index back to the start of a list (i % length), runs something every nth time, and spreads keys across hash table buckets. When negative numbers are possible, remember that the % operator in many languages can return a negative remainder.
A 12-hour clock is arithmetic mod 12. Add the hours, then take the remainder after dividing by 12. So 9 o clock plus 7 hours is 16 mod 12 = 4 o clock. For a 24-hour clock, use mod 24. The same idea gives days of the week with mod 7.
Yes. The remainder is whatever is left after taking out whole multiples of the divisor. So 5.5 mod 2 = 1.5, since 2 fits twice into 5.5 with 1.5 left. The calculator rounds away tiny floating point errors, so results such as 0.3 mod 0.1 show as 0 instead of a long decimal.

Related Tools

GCD and LCM Calculator

Find the greatest common divisor and least common multiple of two or more numbers.

Math and Numbers

Prime Number Checker and Generator

Check if a number is prime and generate lists of prime numbers in any range.

Math and Numbers

Number Base Converter

Convert numbers between binary, octal, decimal, hexadecimal, and any custom base.

Math and Numbers

Exponent and Power Calculator

Calculate powers, exponents, and exponential expressions with support for any base and exponent.

Math and Numbers