Modulo Arithmetic Guide

What is Modulo?

The modulo operation (written a mod n or a % n in programming) gives the remainder after dividing a by n. 17 mod 5 = 2, because 17 = 5×3 + 2. 20 mod 4 = 0 (20 is exactly divisible by 4). 7 mod 7 = 0. For any integer a and positive n: a mod n is always between 0 and n-1. The modulo operation is fundamentally different from division — it discards the quotient and keeps only the remainder.

What's the key thing to understand about Negative Numbers and Modulo?

Different programming languages handle negative modulo differently. In Python: -7 mod 3 = 2 (mathematical/floored modulo — always non-negative). In C, Java, JavaScript: -7 % 3 = -1 (truncated remainder — takes sign of dividend). The mathematical definition (result always non-negative) is generally more useful. -7 mod 3 = 2 because -7 = 3×(-3) + 2. This calculator uses the mathematical (non-negative) convention.

What should I know about Modular Arithmetic Applications?

Clock arithmetic: 'It is 10am, what time will it be in 15 hours?' → (10 + 15) mod 24 = 1am. Days of the week: day 0 = Sunday, if today is Wednesday (day 3), what day is it in 100 days? → (3 + 100) mod 7 = 5 = Friday. Calendar calculations: any repeating cycle uses modulo. Cryptography: RSA encryption is based on modular exponentiation. Checksums: IBAN bank account validation, ISBN book number checks.

What's the key thing to understand about In Programming?

The % operator in most programming languages gives the remainder (modulo). Checking if a number is even: n % 2 == 0. Checking if a number is divisible by k: n % k == 0. Wrapping around an array: index = (index + 1) % arrayLength. Converting between 12-hour and 24-hour time: hour12 = hour24 % 12 || 12. Generating a hash that fits in a table of size n: hash = value % n. Modulo is one of the most frequently used operations in programming.

What should I know about Modulo and Divisibility Rules?

The modulo operation gives a direct way to test whether one number divides another evenly: a mod b = 0 if and only if b divides a exactly, with no remainder. This single fact underlies a huge range of practical checks. Testing whether a number is even or odd is simply n mod 2 (0 means even, 1 means odd). Testing whether a year is a leap year uses modulo repeatedly: divisible by 4 (year mod 4 = 0) and not by 100 (year mod 100 ≠ 0), unless also divisible by 400 (year mod 400 = 0). Checking whether a number is prime involves testing year mod p ≠ 0 for every prime p up to its square root. Many classic divisibility shortcuts are modulo facts in disguise: a number is divisible by 3 if the sum of its digits is divisible by 3, and divisible by 9 under the same digit-sum test, both provable using modular arithmetic properties. These checks are fundamental building blocks in programming, number theory, and basic arithmetic verification.

What's the key thing to understand about the Modulo and Floor Division Relationship?

Modulo isn't an isolated operation — it's mathematically tied to division through a simple identity: for any integer a and positive integer b, a = b × floor(a/b) + (a mod b), where floor(a/b) rounds the division result down to the nearest integer. This means the modulo result is always exactly the 'leftover' amount after taking out as many complete multiples of b as possible. For example, with a = 17 and b = 5: floor(17/5) = floor(3.4) = 3, and 17 = 5×3 + 2, confirming 17 mod 5 = 2. This identity is why modulo is sometimes called 'the remainder of division' — it's literally what's left over after floor division has taken out the whole-number portion. Understanding this relationship explains why modulo results are always smaller than the divisor (0 ≤ a mod b < b for positive b): if the leftover were as large as or larger than b, floor division would simply have taken out one more complete multiple of b.

What do I need to know about Modulo and Checksums (Everyday Error Detection)?

Modular arithmetic quietly underlies many everyday error-detection systems. A UPC barcode's final digit is a checksum calculated so that a specific weighted sum of all the digits, taken mod 10, equals zero — if a scanner computes a different result, it knows a digit was misread or mistyped. ISBN-13 book codes work the same way with mod 10, and credit card numbers use a more elaborate mod-10 check called the Luhn algorithm. These systems work because a single-digit typo or digit transposition almost always changes the weighted sum's remainder, so the checksum fails and the error gets caught immediately — without needing to store or transmit any extra information beyond that one check digit. This is a practical, everyday application of modular arithmetic entirely separate from clock time or cryptography, built on the same "remainder after division" idea this calculator computes directly.

Worked Example: Using Modulo to Find a Day of the Week

Modulo is the standard tool for 'wrap-around' problems, and calendar arithmetic is a classic case. Suppose today is a Wednesday, and the question is what day of the week it will be in 40 days. Assign numbers to the days starting from today: Wednesday = 0, Thursday = 1, Friday = 2, Saturday = 3, Sunday = 4, Monday = 5, Tuesday = 6, and then it wraps back to Wednesday = 7 (which is the same as 0). Since there are 7 days in a week, the day number after 40 days is 40 mod 7. 40 ÷ 7 = 5 remainder 5, so 40 mod 7 = 5. Counting 5 from Wednesday (0,1,2,3,4,5) lands on Monday. So 40 days from a Wednesday is a Monday — which matches simply counting out 5 full weeks (35 days, back to Wednesday) plus 5 more days (Thu, Fri, Sat, Sun, Mon).

How Negative Modulo Differs Across Programming Languages

A frequent source of bugs is that -7 mod 3 does not give the same answer in every system, because languages disagree on whether the remainder should follow the sign of the dividend or the sign of the divisor.

Language / SystemOperatorResult of -7 mod 3Convention
Python%2Result follows the sign of the divisor (floored division)
JavaScript%-1Result follows the sign of the dividend (truncated division)
C / C++ / Java%-1Result follows the sign of the dividend (truncated division)
Mathematics (floor definition)mod2Always non-negative when divisor is positive

This calculator follows the mathematical floor-based convention (the Python-style result), which is the definition most commonly taught and the one that keeps results non-negative when the modulus is positive — the convention generally expected for everyday checks like calendar wrap-around or clock-face arithmetic.

Modulo on a Clock Face: The Most Intuitive Example

A 12-hour clock is a built-in, physical demonstration of modulo 12 arithmetic: when the hour count reaches 12, it wraps back around to 1 rather than continuing to 13. If it is currently 9 o'clock and 8 hours pass, the new hour is (9 + 8) mod 12 = 17 mod 12 = 5 o'clock, matching what a clock actually shows. This 'clock arithmetic' intuition is exactly why modulo is also called clock arithmetic in some textbooks, and it transfers directly to problems like figuring out what time a flight lands after a long duration, before even factoring in time zone changes.

Modulo and Hash Tables

Modulo is the standard trick used inside hash tables (a core data structure in programming) to turn an arbitrary, large hash number into a small array index: index = hash(key) mod table_size. This guarantees the resulting index always falls within the bounds of the underlying array, regardless of how large the original hash value is, which is exactly the 'wrap values into a fixed range' behaviour that makes modulo so useful well beyond pure arithmetic.

Divisibility Rules as Modulo Shortcuts

Many classic mental-maths divisibility rules are really just quick ways of checking whether a number mod some small divisor equals zero, without doing full division.

DivisorShortcut RuleEquivalent Modulo Check
3Digit sum divisible by 3n mod 3 == 0
9Digit sum divisible by 9n mod 9 == 0
11Alternating digit sum divisible by 11n mod 11 == 0

These rules work because of how place value interacts with modular arithmetic — for example, every power of 10 is congruent to 1 mod 9, which is exactly why summing a number's digits gives the same remainder mod 9 as the original number itself, turning a division problem into simple addition.

Modulo Calculator

Results update automatically as you type

Enter values above to calculate