Modulo Calculator

Calculate a mod b instantly: remainder and quotient, floor division included. Mixed-sign inputs show both JS truncated-division and Python floor-mod results side by side.

Remainder

17 mod 5

2

Remainder (JavaScript truncated division)

Quotient (truncated)3
Check5 × 3 + 2 = 17

Dividend and Divisor

The number being divided.

Must be non-zero.

Try These

Quick Answer

a mod b is the remainder after dividing a by b — for example 17 mod 5 = 2 because 17 = 3 × 5 + 2. Enter a and b to get the remainder and quotient instantly. Languages disagree when signs are mixed: JavaScript, Java, and C use truncated division (remainder takes the dividend’s sign: -7 % 3 = -1), while Python and Ruby use floor division (remainder takes the divisor’s sign: -7 mod 3 = 2). This calculator shows both conventions side by side when they differ.

Key Facts

  • a mod b = a − b·q for the relevant quotient q; it answers "what is left over"
  • Truncated division (JavaScript %, Java, C, Go): quotient rounds toward zero, remainder takes the dividend’s sign
  • Floor division (Python //, Ruby): quotient rounds down, remainder takes the divisor’s sign
  • -7 % 3 = -1 in JavaScript but -7 mod 3 = 2 in Python — both satisfy a = b·q + r
  • Division by zero is undefined in every convention: b = 0 is an error, not 0
  • a mod b = 0 exactly when b divides a
  • The two conventions always agree when a and b share the same sign
  • Modular arithmetic wraps results into 0 … b−1 (for positive b), the basis of clock arithmetic and hashing

Frequently Asked Questions

Because "mod" is defined by a = b·q + r, and that leaves freedom in choosing q. Truncated division (JavaScript, Java, C) rounds q toward zero: -7 = 3·(-2) + (-1), so r = -1. Floor division (Python, Ruby) rounds q down: -7 = 3·(-3) + 2, so r = 2. Both are valid remainders satisfying the identity — they differ only in sign convention. This calculator shows both when the operands have different signs.

JavaScript, Java, C, C++, C#, Go, and Rust all use truncated division for %: the quotient is computed with truncation toward zero and the remainder takes the sign of the dividend. Examples: -7 % 3 = -1, 7 % -3 = 1, and the remainder is 0 only when b divides a exactly. Python is the notable exception — its // and % use floor division, so -7 % 3 = 2.

For positive b: if the truncated remainder r is negative, add b to get the floor remainder: floor-mod = ((a % b) + b) % b. Example: -7 % 3 = -1, and -1 + 3 = 2. The quotients differ by exactly 1 (trunc gives -2, floor gives -3). For negative divisors the sign flips the other way — the remainder always takes the divisor’s sign in floor convention.

Everywhere values wrap around: clock arithmetic (17 + 9 hours = 2 o’clock, (17 + 9) mod 24), parity checks (n mod 2), cyclic calendars (leap years: year mod 4/100/400), hashing (index = hash mod table size), circular buffers, ISBN/checksum validation, and cryptography (RSA arithmetic is done mod n).

a mod 0 is undefined: the identity a = 0·q + r forces r = a with no constraint from q, so any q "works" and the remainder is meaningless. Hardware division instructions trap on it, and in JavaScript 5 % 0 returns NaN rather than throwing. This calculator shows a clear error state for b = 0 instead of a number.

Strictly: "remainder" is the truncated-division result (sign of the dividend); "modulus" is the floor-division result (sign of the divisor). In everyday use "modulo" and "%" cover both, which is why mixed-sign cases trip people up. The quotient identity a = b·q + r holds under either definition — only the rounding rule for q changes.