Modulo Calculator

Calculate the modulo (remainder) of any division. Explains the difference between modulo and remainder for negative numbers.

17 mod 5

2

Modulo (a mod b)2
Quotient (truncated)3
Decimal result3.4

Verification

17 = 5 × 3 + 2 (check: 5 × 3 + 2 = 17)

How to Use the Modulo Calculator

  1. Enter the dividend and divisor. The dividend is the number you start with. The divisor is the number you divide by. Both can be positive or negative integers or decimals. Enter 17 and 5, for example.
  2. Read the modulo result. For 17 mod 5, the answer is 2. This means 17 = 5 × 3 + 2. The modulo is always the non-negative remainder when the divisor is positive.
  3. Negative numbers. When either number is negative, different programming languages handle the result differently. JavaScript and C use truncated division (remainder can be negative). Python uses floor division (remainder is always non-negative for positive divisors). The calculator shows all three results when negative inputs are detected.

Common uses: checking if a number is even (n mod 2 = 0), wrapping around a clock (hour mod 12), cycling through array indices, and cryptography (RSA encryption relies on modular arithmetic).

Modulo Formula and Definitions

Basic definition:
  a mod b = a - b × floor(a / b)

For positive a and b:
  17 mod 5 = 17 - 5 × floor(17/5) = 17 - 5×3 = 17 - 15 = 2

Verification identity:
  a = b × q + r
  where q = quotient, r = remainder
  17 = 5 × 3 + 2  ✓

Negative number behavior (a = -7, b = 3):

  Truncated division (C, JavaScript, Java):
    quotient = trunc(-7/3) = trunc(-2.33) = -2
    remainder = -7 - 3×(-2) = -7+6 = -1
    Result: -7 % 3 = -1

  Floor division (Python, Ruby):
    quotient = floor(-7/3) = floor(-2.33) = -3
    remainder = -7 - 3×(-3) = -7+9 = 2
    Result: -7 % 3 = 2 (always non-negative for positive b)

  Mathematical modulo:
    a mod b = ((a % b) + b) % b = ((-1) + 3) % 3 = 2

Common modulo applications:
  n mod 2 = 0   → n is even
  n mod 7       → day of week cycling
  n mod 12      → clock hour (0-11)
  n mod 360     → angle wrapping

Frequently Asked Questions

For positive numbers, modulo and remainder are the same. The difference shows up with negative numbers. In mathematics and Python, modulo is defined using floor division, so the result always has the same sign as the divisor. In JavaScript and C, the % operator uses truncated division, so the result has the same sign as the dividend. For -7 mod 3: Python gives 2, JavaScript gives -1. Neither is wrong; they follow different definitions. For most everyday uses with positive numbers, there is no difference.

Related Calculators