Remainder Calculator
Integer Division & ModuloEnter an integer dividend and divisor to compute the quotient and remainder of the division, verify the identity a = b·q + r, and compare the Euclidean remainder (always non-negative) with a typical programming-style remainder for negative numbers.
Compute quotient and remainder of a ÷ b
Mathematical definition of remainder
Let \(a\) and \(b\) be integers with \(b \ne 0\). A quotient–remainder pair \((q, r)\) satisfies
\[ a = b \cdot q + r. \]
In Euclidean division, which is the standard in number theory, we additionally require
\[ 0 \le r < |b|. \]
Under this condition, the integers \(q\) and \(r\) are unique for any given pair \(a, b\). This is the convention used to define the modulo operation and arithmetic in \(\mathbb{Z}/n\mathbb{Z}\).
Euclidean remainder formula (conceptual)
For a fixed positive divisor \(b > 0\), the Euclidean quotient is \[ q = \left\lfloor \frac{a}{b} \right\rfloor, \] and the remainder is \[ r = a - b \left\lfloor \frac{a}{b} \right\rfloor, \] so that \(0 \le r < b\).
For negative divisors, we can always work with \(|b|\) and then adjust the quotient accordingly.
Remainder vs modulo in programming languages
Many programming languages (C, C++, Java, JavaScript, etc.) define a remainder operator like a % b using a quotient rounded toward zero. That is, they take
\[ q_{\text{trunc}} = \operatorname{trunc}\!\left(\frac{a}{b}\right), \quad r_{\text{trunc}} = a - b \, q_{\text{trunc}}, \]
where trunc drops the fractional part. This makes \(r_{\text{trunc}}\) have the same sign as the dividend \(a\), and it can be negative.
In contrast, the Euclidean remainder \(r\) is defined so that \(0 \le r < |b|\), which is more convenient for mathematical proofs and modular arithmetic.
Worked examples
| a | b | Euclidean q, r | Programming qtrunc, rtrunc | Notes |
|---|---|---|---|---|
| 17 | 5 | q = 3, r = 2 | q = 3, r = 2 | Positive case: both conventions coincide. |
| -17 | 5 | q = -4, r = 3 | q = -3, r = -2 | Euclidean r is non-negative; truncated r follows the sign of a. |
| 17 | -5 | q = -3, r = 2 (since |b| = 5) | q = -3, r = 2 | For this pair, both conventions happen to give the same remainder. |
| -17 | -5 | q = 4, r = 3 (with |b| = 5) | q = 3, r = -2 | Again, Euclidean r is in [0, |b|), truncated r is negative. |
How to interpret the result in practice
- Use the Euclidean remainder for mathematical work, modular arithmetic, congruences, and anything involving \(\bmod\) in textbooks.
- Use the programming-style remainder when you specifically need to mirror the behavior of a concrete language operator (for example, debugging code or porting algorithms).
- When mixing math and code, always state which convention you are using to avoid off-by-one or sign errors.
Typical applications of remainders
- Clock arithmetic (e.g., converting 27 hours into 1:00 using modulo 24).
- Detecting even/odd numbers (checking if a number is congruent to 0 or 1 modulo 2).
- Distributing items into cycles or repeating patterns.
- Hash functions and hashing to array indices.
- Cryptographic algorithms and checksums.
- The Euclidean algorithm for greatest common divisors (GCD).