What Is the Modulo Operation?
The modulo operation finds the remainder after dividing one integer by another. Written as a mod n, it answers the question: when you divide a by n, what is left over? For example, 17 mod 5 equals 2, because 17 divided by 5 is 3 with a remainder of 2. The number being divided is the dividend, and the number you divide by is the modulus or divisor.
Modulo is one of the most useful operations in programming and number theory. It appears in clock arithmetic, hash functions, leap year calculations, scheduling algorithms, and cryptography. The symbol for it varies by context: mathematicians often write a mod n, while programmers use the percent sign (a % n) in languages like C, Java, and Python.
For the full division process with a step-by-step worked solution, use our Long Division Calculator. For factoring and divisibility checks, try the Factor Calculator.
What This Calculator Does
Enter a dividend and a non-zero divisor, and this tool computes the remainder using truncated division, which is the convention used by most programming languages including C, Java, and JavaScript. It also shows the quotient and the step-by-step arithmetic so you can see exactly how the remainder was derived.
- Inputs: A dividend and a non-zero divisor, each between negative one billion and one billion
- Outputs: The remainder, the quotient, and the step-by-step calculation
How the Calculation Works
The modulo operation is defined as: a mod n = a minus (quotient times n), where the quotient is the integer part of a divided by n. This calculator uses truncated division, meaning the quotient is rounded toward zero. For positive numbers this matches what you learned in school. For negative numbers, the result takes the sign of the dividend, which is the C and JavaScript convention.
17 mod 5 = 2 (since 17 = 3 x 5 + 2)
20 mod 7 = 6 (since 20 = 2 x 7 + 6)
10 mod 2 = 0 (since 10 = 5 x 2 + 0)
-17 mod 5 = -2 (truncated division, C/JS convention)
There is a second convention called floored division, used by Python, where the quotient rounds toward negative infinity. Under that convention, -17 mod 5 equals 3, because the result always takes the sign of the divisor. Neither convention is wrong; they serve different purposes. This calculator uses the truncated convention because it matches the behavior of JavaScript, C, and Java, which are the languages most users encounter.
How to Use the Calculator
- Enter the dividend (a) in the first field
- Enter the divisor (n) in the second field; it must not be zero
- The remainder, quotient, and step-by-step breakdown appear instantly
- Use the copy button to copy the remainder
Example Calculations
Example 1: 17 mod 5
A student in Dallas enters 17 and 5. The calculator shows: 17 divided by 5 is 3 (truncated), 3 times 5 is 15, and 17 minus 15 is 2. So 17 mod 5 equals 2. This means if you have 17 items and group them into sets of 5, you have 2 left over. The result is always between 0 and the divisor minus 1 for positive inputs.
Example 2: 100 mod 7 (Days of the Week)
An event planner in Miami needs to know what day of the week an event 100 days from now falls on. She enters 100 mod 7, since there are 7 days in a week. The result is 2, meaning 100 days is 14 full weeks plus 2 extra days. If today is Wednesday, the event will be on Friday. This is the principle behind clock arithmetic, also called modular arithmetic.
Example 3: 2024 mod 4 (Leap Year Check)
A developer in Seattle enters 2024 mod 4 to check whether 2024 is a leap year. The result is 0, which means 2024 is divisible by 4 and is a candidate leap year. The full leap year rule is more complex: a year is a leap year if it is divisible by 4, except for century years which must also be divisible by 400. So 2000 was a leap year but 1900 was not. The modulo operation is the core of this check.
Real-World Scenarios
Hash Tables and Bucket Indexing
Every hash table uses modulo to map a hash value to a bucket index. If a hash table has 1,024 buckets and a key hashes to 98,765, the bucket index is 98,765 mod 1,024, which equals 765. This is why hash table sizes are often chosen as prime numbers: a prime modulus distributes keys more evenly and reduces collisions when keys have arithmetic patterns. A backend engineer at a fintech company in New York might use a prime like 1,009 for the bucket count of a cache storing transaction IDs.
Cryptography and Modular Arithmetic
RSA encryption relies entirely on modular exponentiation. The core operation is raising a number to a power and taking the result modulo a large number. For example, the RSA public key operation computes m to the power e mod n, where n is the product of two large primes. Without the modulo operation, the intermediate results would be astronomically large. The security of RSA depends on the difficulty of reversing this operation without knowing the private key. For the exponent math involved, see our Exponent Calculator.
Cyclic Scheduling and Round-Robin
A load balancer at a cloud provider in Virginia distributes requests across 8 servers using round-robin scheduling. Request number 1,001 goes to server 1,001 mod 8, which is 1 (the second server, zero-indexed). The modulo operation wraps the request counter back to the start of the server list automatically, creating a cycle. This same principle drives time-of-day displays, where the hour is always taken mod 12 or mod 24.
Common Mistakes to Avoid
- Dividing by zero: The modulo operation is undefined when the divisor is zero, just like regular division. This calculator rejects a zero divisor. In programming, attempting a mod by zero typically crashes the program or throws an exception.
- Confusing modulo conventions for negatives: Truncated division (C, Java, JavaScript) gives -17 mod 5 = -2. Floored division (Python) gives -17 mod 5 = 3. Always check which convention your language uses before relying on the sign of a negative modulo result.
- Expecting modulo to equal subtraction: Modulo is not the same as subtracting the divisor once. For 17 mod 5, the answer is 2, not 12. The divisor is multiplied by the full integer quotient, not subtracted just once.
- Forgetting that mod 1 is always 0: Any integer mod 1 equals 0, because every integer is divisible by 1 with no remainder. This is a useful sanity check when debugging modulo-based logic.
Limitations of This Calculator
This calculator handles integers between negative one billion and one billion for both the dividend and divisor. It uses the truncated division convention, matching JavaScript, C, and Java. If you need the floored convention used by Python, the result will differ for negative dividends. This tool does not perform modular exponentiation (a to the power b mod n) or modular inverse calculations, which are common in cryptography. For those, a specialized modular arithmetic tool or a programming language with big-integer support is more appropriate.
Authoritative Research & Resources
- LibreTexts Mathematics - Number Theory - A peer-reviewed open-access resource covering modular arithmetic, the division algorithm, and applications to cryptography. Includes proofs and worked examples at the college level.
- Khan Academy - Cryptography - Free lessons on modular arithmetic and its role in cryptography, including the RSA algorithm and why modular exponentiation is computationally feasible while its inverse is not.
- C++ Reference - Arithmetic Operators - The official reference for the modulo operator in C and C++, documenting the truncated division behavior and the constraint that the divisor must not be zero.
- Python Language Reference - Expressions - Documents Python's floored division convention for the modulo operator, which differs from C and JavaScript for negative operands. Useful for understanding the two conventions.