Modulo Calculator
Calculate the remainder of a division operation. Find a mod b instantly with step-by-step explanation. This free math tool gives instant, accurate results.
What Is the Modulo Operation?
The modulo operation (mod, or %) returns the remainder after dividing one number by another. For a mod b: divide a by b, and the result is the remainder. For example, 17 mod 5 = 2 (because 17 = 3×5 + 2). The result is always in the range [0, b-1] for positive values.
The fundamental relationship: a = q×b + r, where q is the quotient (floor(a/b)) and r is the remainder (0 ≤ r < b). Modulo is the companion operation to integer division — if a ÷ b = q with remainder r, then a mod b = r. This calculator uses the true mathematical modulo definition (always non-negative for positive divisor), rather than the signed remainder used by some programming languages.
Modular arithmetic — arithmetic with a fixed modulus where numbers "wrap around" — forms the basis of clock arithmetic. Hours on a clock are calculated mod 12 or mod 24. If it's 10 AM and you add 5 hours: (10 + 5) mod 12 = 3 (PM). This wrap-around behavior is central to countless algorithms in computer science, cryptography, and number theory.
Modulo Examples and Step-by-Step Solutions
Understanding modulo becomes intuitive with worked examples. For each calculation below, the formula is: remainder = a − floor(a ÷ b) × b.
| Expression | Quotient (floor) | Remainder (a mod b) | Verification |
|---|---|---|---|
| 17 mod 5 | 3 | 2 | 3×5 + 2 = 17 ✓ |
| 20 mod 4 | 5 | 0 | 5×4 + 0 = 20 ✓ |
| 7 mod 3 | 2 | 1 | 2×3 + 1 = 7 ✓ |
| 100 mod 7 | 14 | 2 | 14×7 + 2 = 100 ✓ |
| 13 mod 13 | 1 | 0 | 1×13 + 0 = 13 ✓ |
| 1 mod 5 | 0 | 1 | 0×5 + 1 = 1 ✓ |
| 256 mod 16 | 16 | 0 | 16×16 + 0 = 256 ✓ |
| 365 mod 7 | 52 | 1 | 52×7 + 1 = 365 ✓ |
Notice 365 mod 7 = 1: this tells us that a non-leap year has 52 complete weeks plus 1 extra day, which is why the day of the week shifts by 1 each non-leap year. A leap year (366 days) mod 7 = 2, shifting the day by 2.
Applications of Modular Arithmetic
Modulo appears throughout programming and mathematics. Even/odd check: if n % 2 == 0, n is even. Circular arrays and ring buffers: index = (current_index + 1) % array_size wraps around to the start. Hash tables: bucket = hash(key) % num_buckets maps any hash value to a valid bucket index, ensuring no out-of-bounds access.
In calendar calculations, day-of-week arithmetic uses mod 7. The Zeller formula and Doomsday algorithm both rely on modular arithmetic to determine the day of the week for any date. These work because there are exactly 7 days in a week — a fixed modulus. Time zone offsets use mod 24 to wrap hour values correctly across midnight boundaries.
In digital systems, modulo is used everywhere memory addresses are involved. Page table entries, cache set selection, and memory-mapped I/O all rely on modular indexing. CPU instruction sets typically include a remainder (modulo-like) instruction alongside division, and SIMD vector instructions use modulo for lane wrapping in shuffles.
In error detection, cyclic redundancy checks (CRCs) and checksums are computed using polynomial modular arithmetic over GF(2). Credit card numbers pass the Luhn algorithm (a modulo-10 check). ISBN-10 book numbers use mod 11. These checksums catch transposition and single-digit errors in numeric codes.
Modulo in Cryptography
Modular arithmetic is the mathematical foundation of modern public-key cryptography. The three most important cryptographic algorithms — RSA, Diffie-Hellman, and Elliptic Curve Cryptography — all rely on operations performed modulo a large prime or composite number.
RSA encryption uses modular exponentiation: to encrypt a message M with public key (e, n), compute C = M^e mod n. To decrypt, compute M = C^d mod n where d is the private key. The security relies on the difficulty of factoring n (a large semiprime) — knowing only n, recovering p and q is computationally infeasible for key sizes above 2048 bits.
Diffie-Hellman key exchange allows two parties to establish a shared secret over an insecure channel: Alice sends A = g^a mod p, Bob sends B = g^b mod p. Each party computes the shared secret: Alice computes B^a mod p = g^(ab) mod p, Bob computes A^b mod p = g^(ab) mod p. An eavesdropper who intercepts g^a mod p and g^b mod p cannot recover g^(ab) mod p without solving the discrete logarithm problem.
The security of these systems depends on the one-way nature of modular exponentiation: computing g^a mod p is fast (using repeated squaring, O(log a) multiplications), but reversing it — finding a given g^a mod p — is believed to require exponential time for large primes p.
Modulo with Negative Numbers and Edge Cases
Modulo behavior with negative numbers varies by programming language, which causes many hard-to-find bugs. Understanding the difference is critical for software developers.
| Language | -7 % 3 | 7 % -3 | Definition |
|---|---|---|---|
| Python | 2 | -2 | Sign follows divisor (true modulo) |
| JavaScript | -1 | 1 | Sign follows dividend (remainder) |
| C / C++ | -1 | 1 | Sign follows dividend (C99+) |
| Java | -1 | 1 | Sign follows dividend |
| Ruby | 2 | -2 | Sign follows divisor (true modulo) |
| Math (definition) | 2 | 1 (or undefined) | Always non-negative for positive divisor |
In mathematics, modulo always returns a non-negative result: -7 mod 3 = 2 (since -7 = -3×3 + 2, and 0 ≤ 2 < 3). This calculator uses the mathematical definition.
The safe way to ensure a non-negative result in any language: ((a % b) + b) % b. This handles negative inputs correctly and is used internally by our calculator. This pattern is essential when using modulo for array indexing or calendar day calculations where negative results would cause errors.
Edge cases to remember: (1) Any number mod 1 = 0 — dividing by 1 leaves no remainder. (2) Any number mod itself = 0. (3) 0 mod any non-zero number = 0. (4) Division (and modulo) by zero is undefined — always validate the divisor before computing modulo. Our calculator displays a clear error message for modulo by zero.
Modulo and Divisibility Tests
One of the most practical uses of modulo is testing divisibility without performing full division. A number a is divisible by b if and only if a mod b = 0. This enables fast divisibility checks:
| Divisibility by | Test | Example |
|---|---|---|
| 2 | n mod 2 = 0 (last digit even) | 128 mod 2 = 0 ✓ |
| 3 | Sum of digits mod 3 = 0 | 123: 1+2+3=6, 6 mod 3 = 0 ✓ |
| 4 | Last two digits mod 4 = 0 | 312: 12 mod 4 = 0 ✓ |
| 5 | Last digit is 0 or 5 | 735 mod 5 = 0 ✓ |
| 9 | Sum of digits mod 9 = 0 | 369: 3+6+9=18, 18 mod 9 = 0 ✓ |
| 10 | n mod 10 = 0 (last digit is 0) | 500 mod 10 = 0 ✓ |
These divisibility rules are shortcuts derived from modular arithmetic properties. The digit-sum rules for 3 and 9 work because 10 ≡ 1 (mod 3) and 10 ≡ 1 (mod 9), meaning each digit's positional value is irrelevant for divisibility by 3 or 9. These are taught in elementary school without the modular arithmetic context, but the underlying mechanism is modulo.
Modular Exponentiation: Fast Power Mod
Computing a^b mod n directly by first computing a^b, then taking mod n, is impractical for large exponents — a^100 can have thousands of digits. Modular exponentiation uses the identity (a×b) mod n = ((a mod n) × (b mod n)) mod n to keep intermediate results small.
The fast algorithm uses repeated squaring (binary exponentiation):
- Write b in binary: e.g., b=13 = 1101₂
- Compute a, a², a⁴, a⁸ each taken mod n
- Multiply the powers corresponding to 1 bits: a¹³ = a⁸ × a⁴ × a¹ (mod n)
This reduces the number of multiplications from b to O(log₂ b). For b = 2048-bit RSA exponents (~10^600), this is the difference between trillions of multiplications and just ~2000. Without this optimization, RSA encryption would be completely impractical.
Frequently Asked Questions
What is 15 mod 4?
15 mod 4 = 3. Because 15 = 3×4 + 3, the remainder is 3. Verify: 3×4 = 12, and 15 − 12 = 3. ✓
What does mod 0 mean?
Modulo by zero is undefined, just like division by zero. You cannot calculate a mod 0. Our calculator returns an error message in this case. Any division-based operation requires a non-zero divisor.
How does modulo relate to divisibility?
A number a is divisible by b if and only if a mod b = 0. For example, 24 mod 6 = 0, so 24 is divisible by 6. 25 mod 6 = 1, so 25 is not divisible by 6. This makes modulo the fundamental tool for divisibility testing in computer science.
What is the difference between mod and remainder?
For positive numbers, mod and remainder are identical. For negative numbers, they differ: the mathematical modulo always returns a non-negative result (sign follows the divisor), while the remainder takes the sign of the dividend. For example, -7 mod 3 = 2 (math), but -7 remainder 3 = -1 (as in C, Java, JavaScript).
What is 10 mod 3?
10 mod 3 = 1. Because 10 = 3×3 + 1, the remainder is 1. You can verify: 3×3 = 9, and 10 − 9 = 1. This means 10 leaves a remainder of 1 when divided by 3, so 10 is not divisible by 3.
What is 0 mod 5?
0 mod 5 = 0. Zero divided by any non-zero number gives quotient 0 and remainder 0. In general, 0 mod n = 0 for any n ≠ 0. This is consistent with the definition: 0 = 0×5 + 0.
How is modulo used in programming?
Common programming uses include: checking even/odd (n%2==0), wrapping array indices (index%length), implementing ring buffers, distributing items to buckets in hash tables (hash%size), rotating through states in a state machine, and ensuring periodic events fire on every nth iteration (counter%n==0).
What is clock arithmetic?
Clock arithmetic is everyday modular arithmetic. A 12-hour clock uses mod 12: 11 o'clock + 3 hours = (11+3) mod 12 = 2 o'clock. This wrap-around behavior is precisely modular arithmetic. Similarly, days of the week use mod 7, and military time uses mod 24 for hours.
Why is modulo important in cryptography?
Modular arithmetic makes one-way functions possible. Computing g^a mod p (given g, a, p) is fast, but finding a given g^a mod p and p (the discrete logarithm problem) is computationally infeasible for large primes. This asymmetry underpins Diffie-Hellman key exchange, RSA, and most public-key cryptography protecting internet communications.
What is the result of any number mod 1?
Any integer mod 1 = 0. Dividing by 1 always leaves no remainder — every integer is perfectly divisible by 1. This is mathematically consistent: a = a×1 + 0, so the remainder is always 0. This edge case is important to handle in modular arithmetic implementations.
Modulo in Everyday Life: Practical Examples
Modular arithmetic shows up far more often in daily life than most people realize. The moment you read a clock, calculate when a weekly event recurs, check whether a number is divisible by 9, or look at the last digit of a year to determine what day of the week an anniversary falls on, you're doing modular arithmetic — even if you don't use that name for it.
Scheduling and recurrence: If an event occurs every 7 days and today is Tuesday (day 2, zero-indexed from Sunday=0), then 30 days from now is (2+30) mod 7 = 32 mod 7 = 4, which is Thursday. This direct calculation is faster than counting weeks and days separately. Similarly, if a subscription renews on the 28th of each month and it's currently the 15th, the days until renewal is (28−15) mod 31 = 13 days.
Digital check digits: The ISBN-13 barcode standard uses modulo 10. The last digit of any ISBN-13 is chosen so that the weighted sum of all 13 digits is divisible by 10. If you mistype a single digit when entering a book's ISBN, the check will fail (mod 10 ≠ 0) and an error is flagged. Credit card numbers use the Luhn algorithm — a mod-10 check — for the same purpose. The ISBN-10 standard uses mod 11, allowing detection of single transpositions.
Computer memory and addresses: RAM is typically addressed in powers of 2 (1024, 2048, 4096 bytes per page). When a program accesses memory, the OS uses modulo to calculate which memory page an address falls within: page_number = address mod page_size. Cache line selection in CPU caches uses modulo similarly. Buffer wrap-around in audio processing, network packet queuing, and video streaming all use circular buffer math: write_position = (write_position + 1) % buffer_size.
Art and music patterns: Rhythmic patterns in music theory are analyzed using modular arithmetic. A 4/4 time signature has beats 0, 1, 2, 3 repeating — a mod-4 cycle. Polyrhythms occur when two independent rhythms with periods m and n play simultaneously; they synchronize every lcm(m,n) beats. Visual patterns like tile tessellations repeat with modular periods in two dimensions.
Geographic and time zone calculations: UTC offsets range from −12 to +14. Converting between time zones: given time T in UTC, local time = (T + offset) mod 24. The resulting value might seem unintuitive (e.g., 23 + 5 = 28, mod 24 = 4, meaning 4:00 AM the next day), but the mod operation handles the midnight boundary correctly. International date line crossings use mod 24 in combination with day-of-week calculations using mod 7.
Understanding modulo makes these everyday calculations clearer, faster, and less error-prone. Once you see the pattern, you'll notice modular arithmetic in compiler optimizations, rotation algorithms in video games, round-robin tournament scheduling, and load balancing across server clusters — all relying on the simple but powerful concept of remainder after division.