Modulo Calculator

Remainder (a mod b)
Next

The modulo (or “mod”) operation returns the remainder after integer division: 17 mod 5 = 2 because 17 = 3·5 + 2. It’s the heart of clock arithmetic, cryptography, hash functions and the “every Nth row” pattern in SQL. This calculator accepts integer or decimal operands, shows the quotient and remainder explicitly, and handles the sign-convention differences between languages (Python’s % ≠ C’s % for negatives).

How to compute a mod b

  1. 1

    Enter dividend (a) and divisor (b)

    Any numbers; decimals and negatives OK.

  2. 2

    Pick sign convention

    Truncated division (C, Java, Go), floored division (Python, Ruby, math convention).

  3. 3

    Read quotient and remainder

    a = q·b + r, with the chosen rule for sign of r.

  4. 4

    See the math expression

    Substituted values make each step visible.

Two conventions for negative numbers

For positive operands, all conventions agree: 17 mod 5 = 2. For negatives, languages differ:

Language -17 mod 5 Convention
Python, Ruby 3 Floored
C, Java, Go, JS -2 Truncated
Math textbooks 3 Floored (usually)

Floored division: quotient rounds toward −∞. Remainder always has the sign of the divisor: same-sign as b. Truncated division: quotient rounds toward zero. Remainder has the sign of the dividend: same-sign as a.

For a = -17, b = 5:

  • Truncated: q = -3 (rounded toward 0), r = -17 − (-3)·5 = -2.
  • Floored: q = -4 (rounded toward -∞), r = -17 − (-4)·5 = 3.

Both are correct given the convention. Pick to match your language.

Where modulo shows up

  • Time. Minutes in an hour, days of the week, seconds in a day, all mod arithmetic.
  • Hash tables. hash(key) mod table_size picks a bucket.
  • Round-robin scheduling. task_i mod worker_count assigns work.
  • Cryptography. RSA and Diffie-Hellman are built on mod n with huge primes.
  • Every Nth row. In SQL: WHERE id % 3 = 0.
  • Even/odd check. n mod 2 == 0 means even.
  • Alternating patterns. Row striping, coloring every other item.
  • Circular buffers. (index + 1) mod size wraps around.

Useful identities

  • (a + b) mod n = ((a mod n) + (b mod n)) mod n
  • (a · b) mod n = ((a mod n) · (b mod n)) mod n
  • (a^k) mod n can be computed by fast modular exponentiation in O(log k), critical for large-number crypto.
  • a mod 1 = 0 for any integer a.
  • a mod a = 0.

Decimal modulo

For real numbers, the natural definition is a mod b = a − b · floor(a/b). 7.5 mod 2.5 = 0 because 7.5 is an exact multiple. 7.6 mod 2.5 = 0.1.

JavaScript’s % operator works on real numbers; Python’s fmod does truncated real mod; Python’s % does floored real mod.

Worked examples

  • 100 mod 7: 100 = 14·7 + 2, so remainder 2.
  • 25 mod 4: 25 = 6·4 + 1, so remainder 1.
  • -10 mod 3 (floored): -10 = -4·3 + 2, remainder 2. (Truncated would give -1.)
  • 17.5 mod 5: 17.5 = 3·5 + 2.5, remainder 2.5.

Frequently Asked Questions

Most of the time they’re synonyms. Formally, “modulo” often implies the mathematical (floored) convention, while “remainder” often refers to the truncated (C-style) variant. The names leak into language docs; when it matters, spell out the convention.

Python follows the math convention (floored), which keeps the remainder’s sign matching the divisor. C follows the hardware ALU convention (truncated), where the remainder’s sign matches the dividend. Neither is wrong; they’re different choices.

No, division by zero is undefined. The calculator returns an error for b = 0.

Usually, yes. Check your language’s spec for negative-number handling, Python and Ruby differ from C, Java, Go and JavaScript.

For RSA-style math with 2048-bit numbers, use a dedicated library (Python’s pow(a, b, n), Java’s BigInteger.modPow). This calculator handles everyday values, not crypto-grade ones.

Related Tools

Tool available in other languages