Modulo Calculator | Remainder Division
Calculate the remainder of a division (modulo operation) for any two numbers.
Floored: remainder always ≥ 0, matches Python % and mathematical modulo.
What Is the Modulo Calculator | Remainder Division?
The Modulo Calculator finds the remainder when dividing any two numbers, supporting both the Floored convention (Python %, always non-negative) and the Truncated convention (C, Java, JavaScript %, sign follows dividend). When the two conventions produce different results, which only happens for negative inputs, both are shown side-by-side for comparison.
- ›Two conventions: toggle between Floored (mathematical modulo, remainder always ≥ 0) and Truncated (matches C/JavaScript %, remainder sign matches dividend).
- ›Side-by-side comparison: when results differ (negative dividend), both Floored and Truncated values are displayed together.
- ›Divisibility check: instantly shows whether a is exactly divisible by b (remainder = 0).
- ›Even/odd detection: for integer dividend and |b| = 2, reports whether the dividend is even or odd.
- ›Modulo lookup table: for integer divisors ≤ 20, displays n mod b for all n from 0 to |b|−1.
- ›Step-by-step working: shows the division, quotient calculation, remainder, and verification step a = q×b + r.
Formula
| Convention | Quotient | Remainder sign | Languages |
|---|---|---|---|
| Floored (math) | q = floor(a/b) | Always ≥ 0 (matches sign of b) | Python %, math |
| Truncated | q = trunc(a/b) | Matches sign of dividend a | C, Java, JavaScript % |
| Euclidean | q = sign(b)·floor(a/|b|) | Always ≥ 0 | Haskell mod, Ada mod |
| Ceiling | q = ceil(a/b) | Non-positive when b > 0 | Less common |
How to Use
- 1Select a convention: "Floored (Python, math)" for always-non-negative remainders, or "Truncated (C, Java, JS %)" to match most programming languages.
- 2Enter the dividend (the number being divided) in the first field.
- 3Enter the divisor (the number dividing by) in the second field, must be nonzero.
- 4Click "Calculate", or press Enter, to compute the remainder.
- 5Read the large result at the top: this is a mod b using the selected convention.
- 6Check the result cards for quotient, remainder, and divisibility.
- 7If results differ between conventions (negative inputs), see the "Both conventions" panel.
Example Calculation
Example 1, Positive numbers (both conventions agree)
Example 2, Negative dividend (conventions differ)
Which convention should I use?
Understanding Modulo | Remainder Division
The Division Algorithm and Modulo
The Division Algorithm states that for any integers a (dividend) and b ≠ 0 (divisor), there exist unique integers q (quotient) and r (remainder) such that a = q·b + r and 0 ≤ r < |b|. This is the floored modulo. The key property is that the remainder r always satisfies 0 ≤ r < |b|, it is non-negative and strictly less than the absolute value of the divisor.
Different programming languages implement this differently, leading to two main conventions in practice. The floored convention (used in Python and mathematics) always produces a non-negative remainder. The truncated convention (C, Java, JavaScript) produces a remainder whose sign matches the dividend. They agree for positive numbers and differ only when the dividend is negative.
- ›For positive a and b: both conventions give the same result.
- ›For negative a and positive b: floored gives r ≥ 0; truncated gives r ≤ 0.
- ›For positive a and negative b: floored gives r ≤ 0; truncated gives r ≥ 0.
- ›For negative a and negative b: floored gives r ≤ 0; truncated gives r ≥ 0.
Modular Arithmetic and Congruences
Modular arithmetic is the study of remainders under a fixed modulus n. We say a ≡ b (mod n), "a is congruent to b modulo n", when a and b have the same remainder when divided by n, i.e., n divides (a − b). Modular arithmetic has the same addition and multiplication properties as ordinary arithmetic, making it extremely useful in number theory, cryptography, and computing.
- ›Closure: if a ≡ r (mod n) and b ≡ s (mod n), then (a+b) ≡ (r+s) (mod n) and a·b ≡ r·s (mod n).
- ›Clock arithmetic: clocks use modulo 12 (or 24). 11 + 3 = 14 ≡ 2 (mod 12).
- ›Day of week: adding 7 days is modulo 7, any multiple of 7 days away is the same weekday.
- ›Hash tables: hash(key) mod tableSize gives the array index, distributing keys evenly.
Modulo in Programming and Cryptography
Modulo is one of the most-used operations in computer science. It appears in array index wrapping, hash functions, random number generation, cyclic iterators, and the core of public-key cryptography.
- ›Even/odd test: n % 2 === 0 is true iff n is even, the most common use of modulo.
- ›Circular indexing: (i + 1) % n wraps an index back to 0 after reaching n−1 (e.g., circular buffers, next slide in a carousel).
- ›RSA encryption: relies on modular exponentiation, computing aᵉ mod n efficiently using repeated squaring.
- ›Checksums: Luhn algorithm (credit card validation) and ISBN check digits use modulo 10 arithmetic.
- ›Cryptographic hashing: compression functions in SHA and MD5 rely on modular addition and XOR.
Floored vs Truncated, Why It Matters
The difference between floored and truncated modulo only surfaces with negative numbers, but it can cause subtle bugs when switching between languages. If you compute a range or wrap index with a negative offset and expect a non-negative result, use floored modulo. In JavaScript (truncated %), the fix is: ((a % b) + b) % b, this always returns a non-negative result matching floored modulo for integer b > 0.
- ›Python built-in % is floored, -7 % 3 = 2.
- ›JavaScript % is truncated, -7 % 3 === -1.
- ›Java % is truncated; use Math.floorMod(a, b) for floored since Java 8.
- ›C % is truncated (since C99); C++ follows the same.
- ›Haskell: mod is floored; rem is truncated, two separate operators.
Frequently Asked Questions
What does the modulo operation do?
The modulo operation finds the remainder after dividing one number by another. Written as a mod b (or a % b in most programming languages), it answers: after dividing a by b, what is left over?
For example, 17 mod 5 = 2 because 17 = 3×5 + 2. The number 17 contains three complete groups of 5, with 2 remaining. The result always lies between 0 (inclusive) and |b| (exclusive) when using the floored (mathematical) convention.
- ›17 mod 5 = 2 (17 = 3×5 + 2)
- ›20 mod 4 = 0 (20 = 5×4 + 0, exactly divisible)
- ›7 mod 10 = 7 (7 = 0×10 + 7, dividend smaller than divisor)
Why does −7 mod 3 equal 2 in Python but −1 in JavaScript?
Python uses floored division (quotient rounded toward negative infinity), while JavaScript uses truncated division (quotient rounded toward zero). This only makes a difference for negative numbers:
Both satisfy the equation a = q×b + r; they just choose different quotients. To get floored modulo in JavaScript: ((a % b) + b) % b.
How is modulo used in programming?
Modulo is one of the most frequently used operations in programming:
- ›Even/odd check: n % 2 === 0 returns true for even numbers.
- ›Circular indexing: (i + 1) % n wraps around from the last to the first element.
- ›Rate limiting: trigger an action every k iterations: if (i % k === 0).
- ›Time calculations: totalMinutes % 60 gives the minutes part of a duration.
- ›Hash tables: hash(key) % tableSize maps keys to array indices.
- ›Cryptography: modular exponentiation is the core of RSA and Diffie-Hellman key exchange.
What is modular arithmetic?
Modular arithmetic is a system of arithmetic for integers where numbers "wrap around" after reaching a certain value (the modulus). Two numbers are congruent modulo n (written a ≡ b mod n) if they have the same remainder when divided by n.
Clock arithmetic is the most familiar example: on a 12-hour clock, 10 + 5 = 15 ≡ 3 (mod 12). The set of remainders {0, 1, 2, ..., n−1} forms a complete residue system modulo n. Addition, subtraction, and multiplication all work consistently within this system, enabling powerful algebraic structures used in number theory and cryptography.
What is the difference between remainder and modulo?
For positive numbers, "remainder" and "modulo" mean the same thing. The distinction appears only for negative numbers, depending on how the quotient is rounded:
- ›Remainder (truncated division): sign matches the dividend, used in C, Java, JavaScript.
- ›Modulo (floored division): always non-negative, used in Python, mathematics, number theory.
- ›Both satisfy a = quotient × divisor + result.
- ›For positive a and b, remainder = modulo, no difference.
In formal mathematics, "modulo" almost always means the floored (non-negative) version. In everyday programming, the term is used loosely to mean whichever behaviour the language implements.
How do you check if one number is divisible by another using modulo?
A number a is divisible by b if and only if a mod b = 0, there is no remainder. This is the definition of divisibility:
Common divisibility tests use modulo: divisible by 2 iff last digit mod 2 = 0; divisible by 5 iff last digit mod 5 = 0; divisible by 9 iff sum of digits mod 9 = 0.
What is modular exponentiation and why is it important?
Modular exponentiation computes aᵉ mod n efficiently, without needing to compute the (potentially astronomical) value of aᵉ first. It uses the "square and multiply" (fast exponentiation) algorithm:
- ›Write e in binary.
- ›Square and reduce mod n at each bit.
- ›Multiply by a and reduce mod n when the bit is 1.
- ›Result: aᵉ mod n in O(log e) multiplications.
This is the foundational operation of RSA encryption (computing ciphertext as mᵉ mod n and decryption as cᵈ mod n) and Diffie-Hellman key exchange. Without fast modular exponentiation, modern public-key cryptography would be computationally infeasible.