Division does not always come out even. When 17 is divided by 5, the result is 3 with 2 left over. That leftover — the remainder — is not a loose end to discard. It carries information about the relationship between the two numbers, and an entire branch of arithmetic is built around extracting and using it.
What is Modulo?
The modulo operation takes two integers and returns the remainder of their division. Given integers a and n (with n>0), "amodn" answers one question: what is left over when a is divided by n?
The answer comes from the division equation that connects any integer a to a divisor n:
a=n⋅q+r,0≤r<n
The integer q is the quotient — how many complete copies of n fit inside a. The integer r is the remainder — what remains after those copies are removed. Modulo gives r.
The expression 17mod5=2 because 17=5⋅3+2. Three complete groups of 5 account for 15, leaving 2.
The expression 23mod7=2 because 23=7⋅3+2. Three complete groups of 7 account for 21, leaving 2.
The expression 12mod4=0 because 12=4⋅3+0. The division is exact — nothing remains.
Notation
Mathematical notation writes the operation as amodn. The expression is read "a modulo n" or simply "a mod n."
Most programming languages use the percent symbol instead: a%n. In C, Java, Python, JavaScript, and nearly every other language, the % operator computes remainders. The syntax differs from the mathematical form, but the underlying operation is the same.
One notational subtlety deserves attention early. The expression amodn denotes an operation that produces a number — a remainder. The expression a≡b(modn), which appears later on this page, denotes a relationship between two numbers. The first is a computation; the second is a statement. Both use "mod," but they play different grammatical roles.
Modulo and Divisibility
Modulo and divisibility are two sides of the same coin. Divisibility asks whether one integer divides another exactly. Modulo computes the evidence.
When amodn=0, the remainder vanishes — n divides a with nothing left over. In divisibility notation, n∣a. The statement 24mod6=0 and the statement 6∣24 express identical facts in different languages.
When amodn=0, the remainder is positive — n does not divide a exactly. The statement 25mod6=1 tells us 6∤25, and it tells us by how much the division misses.
Divisibility is the concept — the yes-or-no question of whether one number fits evenly into another. Modulo is the computational tool — it answers the question and, when the answer is no, quantifies the shortfall.
The Range of Remainders
For a fixed divisor n, the remainder amodn is always one of the values {0,1,2,…,n−1}. There are exactly n possibilities, and no remainder can reach n itself — if it did, another complete copy of n could be extracted.
When n=2, every integer produces a remainder of 0 or 1. This is the basis of even and odd: even numbers give 0, odd numbers give 1.
When n=10, every integer produces a remainder between 0 and 9. That remainder is the last digit of the number — 347mod10=7, read directly from the ones place.
When n=7, every integer maps to one of seven remainders: {0,1,2,3,4,5,6}. No matter how large the dividend grows — thousands, millions, billions — the remainder when divided by 7 never escapes this set.
The modulo operation compresses the entire number line into a finite set of n values, repeating in cycles. Every integer lands somewhere in {0,1,…,n−1}, and infinitely many integers share each landing spot.
How to Compute a Remainder
To find amodn, divide a by n, discard the fractional part of the quotient, multiply back, and subtract. The gap between the original number and the nearest multiple of n below it is the remainder.
For 47mod5: divide 47÷5=9.4. The integer part is 9. Multiply back: 9×5=45. Subtract: 47−45=2. The remainder is 2.
For 123mod7: divide 123÷7=17.57…. The integer part is 17. Multiply back: 17×7=119. Subtract: 123−119=4. The remainder is 4.
The procedure has a compact formula using the floor function ⌊x⌋, which gives the greatest integer less than or equal to x:
amodn=a−n⋅⌊na⌋
This captures the three-step process in a single expression: divide, truncate to an integer, subtract the product.
Edge Cases
Three cases deserve attention before the operation is applied routinely.
When a<n, the divisor exceeds the dividend and fits zero times. The entire value of a is the remainder: 3mod10=3, 0mod5=0, 6mod7=6. If 0≤a<n, then amodn=a — the number already lies within the remainder set and needs no reduction.
When n=1, every integer is a multiple of 1, so the remainder is always 0. The expression amod1=0 for any integer a. The remainder set {0,1,…,n−1} collapses to {0}.
When n=0, the operation is undefined. Division by zero has no meaning, and "the remainder when dividing by zero" inherits that meaninglessness. No quotient q satisfies a=0⋅q+r, because 0⋅q=0 regardless of q. In programming, attempting a%0 raises a runtime error.
Negative Dividends
Everything above assumes a is non-negative. When the dividend is negative, the meaning of "remainder" splits — two conventions exist, each producing a different answer to the same question.
The expression (−7)mod3 equals −1 under one convention and 2 under another. Both satisfy the division equation a=n⋅q+r; they differ in how the quotient q is rounded, which forces different values of r.
The distinction matters in practice because different programming languages adopt different conventions — code that works correctly in Python may produce different results in Java or JavaScript for the same negative input.
The full treatment — both conventions, their rationale, language-specific behavior, and conversion between them — is on the negative numbers page.
Congruence
When two integers share the same remainder under division by n, they are called congruent modulo n. The notation is:
a≡b(modn)
This reads "a is congruent to b modulo n" and means amodn=bmodn — both numbers land on the same remainder.
The expression 17≡5(mod12) holds because 17mod12=5 and 5mod12=5. Both leave a remainder of 5 when divided by 12.
The expression 23≡2(mod7) holds because 23mod7=2 and 2mod7=2. Both leave a remainder of 2.
An equivalent way to test congruence: a≡b(modn) if and only if n∣(a−b) — their difference is divisible by n. For the first example, 17−5=12, and 12∣12. For the second, 23−2=21, and 7∣21.
Congruence is not a new operation. It is a way of saying "same remainder" — a relationship between two numbers with respect to a shared divisor.
Properties of Congruence
Congruence modulo n behaves like equality in several important ways. Three properties establish it as an equivalence relation — a formal notion of "sameness" within the modular world.
Reflexive: every integer is congruent to itself. a≡a(modn) for any a and n, because a−a=0 and n∣0.
Symmetric: if a≡b(modn), then b≡a(modn). If n divides a−b, it also divides b−a.
Transitive: if a≡b(modn) and b≡c(modn), then a≡c(modn). If n divides both a−b and b−c, it divides their sum (a−b)+(b−c)=a−c.
These properties mean congruence partitions the integers into groups — called congruence classes — where every integer in a group shares the same remainder. For n=3, the integers split into three classes: those with remainder 0 (multiples of 3), those with remainder 1, and those with remainder 2. Every integer belongs to exactly one class.
The Clock Analogy
A 12-hour clock is modular arithmetic made physical. After 12 o'clock comes 1 o'clock, not 13 — the numbers wrap around.
The time 15:00 on a 24-hour clock translates to 3:00 on a 12-hour clock: 15mod12=3. The time 27 hours from midnight is 3:00 AM: 27mod12=3. Different numbers, same position on the dial.
Days of the week follow the same pattern with n=7. Starting from Sunday (0), day 10 of a sequence falls on the same weekday as day 3: 10mod7=3, which is Wednesday. Day 17 is also Wednesday. Day 24 is Wednesday again. The cycle repeats every 7 days.
The general principle: any system that cycles with period n is governed by modular arithmetic. Months cycle with period 12. Musical notes (in Western chromatic tuning) cycle with period 12. Degree measurements on a compass cycle with period 360. In each case, modulo captures the wrap-around — reducing an unbounded count to a position within a fixed cycle.
Why Modulo Matters
At its simplest, modulo tests divisibility — check whether the remainder is zero. But the operation reaches far beyond that single question.
Pattern recognition depends on modulo. The last digit of any number is that number mod10. The last two digits are the number mod100. Identifying whether a number is even or odd is a mod2 test. These are modular observations embedded so deeply in everyday arithmetic that they rarely get named as such.
Many divisibility rules are modular arguments in compact form. The rule that a number is divisible by 9 if its digit sum is divisible by 9 rests on the fact that 10≡1(mod9). The rule for 11 uses 10≡−1(mod11). Modulo provides the machinery; the rules are shortcuts derived from it.
In computer science, modulo controls array indexing, hash functions, and cyclic data structures. In cryptography, modular arithmetic underpins the algorithms that secure digital communication. The humble remainder, extracted from a simple division, turns out to be one of the most widely used tools in all of mathematics.
Section Roadmap
The pages that follow develop modulo in two directions.
Modular Arithmetic builds a self-contained system where addition, subtraction, multiplication, and exponentiation all operate within the confines of a fixed modulus. These tools solve problems involving last digits, cyclic patterns, and large powers that would be impractical to compute outright.
Negative Numbers addresses the convention split that arises when the dividend is negative — two valid definitions, different answers, and different behavior across programming languages.
Related Sections
Modulo connects to several areas of arithmetic that approach the same ideas from different angles.
Divisibility is the concept that modulo tests. The statement amodn=0 is the computational form of n∣a. Every divisibility question is, at its core, a modulo question.
Divisibility rules are modular arguments packaged as mental shortcuts. The digit-sum test for 9, the last-digit test for 5, the alternating-sum test for 11 — each one exploits a congruence property of 10 raised to successive powers.
The GCD — greatest common divisor — relies on modulo through the Euclidean algorithm, which repeatedly applies the modulo operation to reduce a pair of numbers until the remainder reaches zero. The last nonzero remainder is the GCD.