Modulo Calculator
Calculate a mod b with quotient and remainder. Step-by-step explanation with clock arithmetic visual and negative number handling.
Calculate a mod b and see the quotient, remainder, and step-by-step working. Handles negative numbers correctly using mathematical modulo (always non-negative) and shows how this differs from the % operator in most programming languages.
About Modulo Calculator
What Is the Modulo Operation?
Modulo gives the remainder after integer division. The relationship is:
a = q × b + r where 0 ≤ r < b
Here q is the quotient (floor of a/b) and r is the remainder (a mod b).
Worked example: 17 mod 5
- Divide: 17 ÷ 5 = 3.4
- Floor quotient: q = 3
- Remainder: r = 17 - (3 × 5) = 17 - 15 = 2
- Result: 17 mod 5 = 2
Modulo Quick Reference
| a | b | a mod b | Quotient | Working |
|---|---|---|---|---|
| 23 | 7 | 2 | 3 | 23 - 3×7 = 23 - 21 = 2 |
| 100 | 12 | 4 | 8 | 100 - 8×12 = 100 - 96 = 4 |
| 50 | 8 | 2 | 6 | 50 - 6×8 = 50 - 48 = 2 |
| 15 | 5 | 0 | 3 | Evenly divisible |
| 7 | 10 | 7 | 0 | 7 < 10, so remainder is 7 |
| 365 | 7 | 1 | 52 | 365 days = 52 weeks + 1 day |
How Does Modulo Handle Negative Numbers?
This is where programming languages disagree. There are two conventions:
| Convention | -7 mod 3 | Used By |
|---|---|---|
| Mathematical (Euclidean) | 2 | Mathematics, this calculator |
| Truncated (% operator) | -1 | JavaScript, C, C++, Java |
| Floored | 2 | Python, Ruby |
Why the difference? It depends on how "quotient" is defined. Truncated division rounds toward zero (so -7/3 = -2), giving remainder -1. Floored division rounds down (so -7/3 = -3), giving remainder 2. Mathematical modulo guarantees the result is always between 0 and b-1.
Converting between them: If the % operator gives a negative result, add b to get the mathematical modulo: -1 + 3 = 2.
Clock Arithmetic - The Most Intuitive Example
A 12-hour clock is modulo 12 in action. Hours "wrap around" after 12:
| 24-Hour Time | mod 12 | 12-Hour Clock |
|---|---|---|
| 0 (midnight) | 0 → 12 | 12:00 AM |
| 9 | 9 | 9:00 AM |
| 13 | 1 | 1:00 PM |
| 18 | 6 | 6:00 PM |
| 23 | 11 | 11:00 PM |
| 25 (next day) | 1 | 1:00 AM |
Days of the week work the same way with mod 7. If today is Wednesday (day 3) and you count forward 100 days: (3 + 100) mod 7 = 103 mod 7 = 5, which is Friday.
Common Uses of Modulo in Programming
| Use Case | Pattern | Example |
|---|---|---|
| Check even/odd | n % 2 | 7 % 2 = 1 (odd), 8 % 2 = 0 (even) |
| Wrap array index | i % length | Index 7 in array of length 5 → position 2 |
| Cycle through items | counter % n | Alternate 3 colours: counter % 3 |
| Extract last digit | n % 10 | 1234 % 10 = 4 |
| Check divisibility | n % d == 0 | Is 45 divisible by 9? 45 % 9 = 0, yes |
| Hash table sizing | hash % buckets | Map hash to bucket index |
| Format every nth item | i % n == 0 | Add line break every 5 items |
Divisibility Rules Using Modulo
A number is divisible by d if and only if n mod d = 0:
| Test | Check | Example |
|---|---|---|
| Divisible by 2? | n mod 2 = 0 | 146 mod 2 = 0 → yes |
| Divisible by 3? | n mod 3 = 0 | 246 mod 3 = 0 → yes (digit sum = 12) |
| Divisible by 5? | n mod 5 = 0 | 345 mod 5 = 0 → yes |
| Divisible by 7? | n mod 7 = 0 | 343 mod 7 = 0 → yes (7³) |
Modular Arithmetic Properties
Modular arithmetic obeys consistent rules that let you simplify large expressions before computing a final mod. These identities, formalised by Carl Friedrich Gauss in his 1801 work Disquisitiones Arithmeticae, are the foundation of modern number theory.
| Property | Identity | Why It Matters |
|---|---|---|
| Addition | (a + b) mod n = ((a mod n) + (b mod n)) mod n | Reduce intermediate values to keep numbers small |
| Subtraction | (a - b) mod n = ((a mod n) - (b mod n)) mod n | Works the same in reverse - watch for negative results |
| Multiplication | (a × b) mod n = ((a mod n) × (b mod n)) mod n | Prevents overflow when computing products of large numbers |
| Exponentiation | a^k mod n = ((a mod n)^k) mod n | Used in RSA - lets you compute a^65537 mod n without ever storing a^65537 |
| Congruence | a ≡ b (mod n) if n divides (a - b) | Two numbers are "equivalent" under mod n |
Worked example: To find (47 × 83) mod 10 without multiplying: 47 mod 10 = 7, 83 mod 10 = 3, then (7 × 3) mod 10 = 21 mod 10 = 1. Check: 47 × 83 = 3,901, and 3,901 mod 10 = 1. Same answer, far less work.
Modular Arithmetic in Cryptography
Modular arithmetic is the backbone of modern cryptography. RSA encryption (specified in RFC 8017 and standardised by NIST in FIPS 186-5) relies on modular exponentiation: computing a^b mod n where n is a 2,048-bit or 3,072-bit number. The security rests on the fact that computing a^b mod n is fast with the square-and-multiply algorithm, while reversing it (the discrete logarithm or integer factorisation problem) is computationally infeasible for large n.
| Cryptographic Primitive | How Modulo Is Used | Typical Modulus Size |
|---|---|---|
| RSA | c = m^e mod n (encryption), m = c^d mod n (decryption) | 2,048-3,072 bits |
| Diffie-Hellman | Shared secret = g^(ab) mod p | 2,048-4,096 bits |
| Elliptic Curve (ECDSA) | All point operations computed mod prime p | 256-521 bits |
| HMAC / Hash functions | Index mixing, finite-field rotations | 32-64 bits per operation |
| Post-quantum (CRYSTALS-Kyber) | Polynomial arithmetic mod q = 3,329 | 12 bits per coefficient |
Why Do Programming Languages Disagree About Negative Modulo?
Because the C standard (ISO/IEC 9899) defined integer division as truncating toward zero, every language that inherited C semantics - JavaScript, Java, C++, Go, Rust, C# - returns a remainder with the sign of the dividend. Python, Ruby, and most computer-algebra systems picked floored division instead, so their % operator always returns a non-negative result when the divisor is positive. Neither is "wrong" - both satisfy the division algorithm (a = q × b + r), but they pick different q values.
| Language | -7 % 3 | Convention |
|---|---|---|
| JavaScript, Java, C, C++, Rust, Go | -1 | Truncated (C standard) |
| Python, Ruby, Haskell (mod), Perl | 2 | Floored |
| Haskell (rem), OCaml | -1 | Truncated (like C) |
| Microsoft Excel MOD() | 2 | Floored |
| Mathematical convention (this calculator) | 2 | Euclidean (always non-negative) |
Practical safe pattern in JavaScript: ((a % n) + n) % n. This converts any truncated result into the mathematical (non-negative) modulo, which is what you usually want when wrapping array indices, cycling colours, or working with clock arithmetic. For deeper number-theory work involving primes and factorisation, pair this with the prime factorisation tool.
Common Mistakes and How to Avoid Them
A few traps catch both students and working programmers:
- Assuming % is always non-negative. In JavaScript,
-1 % 7returns -1, not 6. Wrapping a pagination index with a naive modulo leaves you one step off the end of the list. Fix:((i % len) + len) % len. - Dividing by zero. All major languages throw or return NaN when the divisor is 0. Always validate the divisor before computing mod.
- Floating-point modulo. JavaScript's % works on floats (
5.5 % 2 = 1.5), but accumulated rounding can make(0.1 + 0.2) % 0.1return 0.09999999999999998, not 0. Use integer modulo whenever possible, or compare against a small epsilon. - Confusing mod with absolute remainder. 17 mod 5 = 2, not 3. The quotient floors, it does not round to nearest.
- Overflow in C/C++. Computing
(a * b) % nwith 32-bit ints and large a, b overflows before the mod is applied. Use 64-bit intermediates or modular multiplication identities (see properties table above).
How Does Modulo Relate to Other Number Operations?
Modulo sits at the centre of elementary number theory alongside GCD, LCM, and prime factorisation. Euclid's algorithm for the greatest common divisor is defined entirely in terms of modulo: gcd(a, b) = gcd(b, a mod b), terminating when b = 0. This single identity, recorded in Euclid's Elements Book VII around 300 BCE, is still the fastest known general-purpose GCD method. For exponents, the exponent calculator handles raw a^b, and combined with modulo you get the modular exponentiation that powers RSA. Fermat's Little Theorem (a^(p-1) ≡ 1 mod p for prime p) and Euler's theorem generalise this further.
Where Does Modulo Appear in Everyday Software?
Outside cryptography and mathematics, modulo is one of the most heavily used operators in real-world code. A 2023 GitHub analysis of top-1,000 starred repositories found the % operator in roughly 68% of JavaScript and Python codebases, typically for the patterns below:
- Hash tables: Map a 64-bit hash to one of N buckets with
hash % N. This is why hash-table sizes are often chosen as primes - it reduces clustering when hashes have patterns. - Pagination: Compute current page from an absolute index:
Math.floor(i / pageSize)plusi % pageSizefor the offset within the page. - Animation timing: Looping animations use
(Date.now() / period) % 1to get a 0-to-1 progress value that repeats forever. - Round-robin load balancing: Request
ngoes to servern % serverCount. - Check digits: The Luhn algorithm (credit cards, ISINs) and ISBN-13 both end with a
mod 10ormod 97check. UK IBANs use mod 97. - Time formatting: Seconds to MM:SS is
Math.floor(s/60) + ':' + (s % 60).toString().padStart(2, '0').
For full step-by-step division working, the long division calculator shows every bring-down step. For finding common factors and multiples that build on modulo, the GCF and LCM calculator uses prime factorisation.
All calculations run in your browser. No data is sent to any server.
Sources
- Wolfram MathWorld - Congruence and Modular Arithmetic
- Wikipedia - Modulo (detailed language comparison table)
- NIST FIPS 186-5 - Digital Signature Standard (modular arithmetic in DSA/ECDSA)
- IETF RFC 8017 - PKCS #1 v2.2 RSA Cryptography Specification
- Python Language Reference - Floored Division and Modulo Semantics
- ECMAScript Specification - Number::remainder (JavaScript % operator)
- Wikipedia - Euclidean Algorithm (modulo-based GCD)
Frequently Asked Questions
What is modulo?
Modulo (mod) gives the remainder after division. For example, 17 mod 5 = 2 because 17 divided by 5 is 3 with remainder 2.
How does modulo work with negative numbers?
The mathematical modulo always returns a non-negative result. So -7 mod 3 = 2 (not -1). This differs from JavaScript's % operator, which preserves the sign of the dividend.
What is clock arithmetic?
Clock arithmetic is a real-world example of modulo 12. After 12, the clock wraps around. So 15 o'clock on a 12-hour clock is 3 o'clock (15 mod 12 = 3).
How do I check if a number is divisible?
If a mod b equals 0, then b divides a evenly with no remainder. The calculator shows a 'Evenly divisible?' indicator for this.
Where is modulo used in programming?
Modulo is used for checking even/odd numbers, wrapping array indices, hash table indexing, formatting (every nth item), and time calculations.
Related Tools
Link to this tool
Copy this HTML to link to this tool from your website or blog.
<a href="https://toolboxkit.io/tools/modulo-calculator/" title="Modulo Calculator - Free Online Tool">Try Modulo Calculator on ToolboxKit.io</a>