Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Modulo






The Mathematics of Remainders

Division does not always come out even. When 1717 is divided by 55, the result is 33 with 22 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 aa and nn (with n>0n > 0), "amodna \bmod n" answers one question: what is left over when aa is divided by nn?

The answer comes from the division equation that connects any integer aa to a divisor nn:

a=nq+r,0r<na = n \cdot q + r, \qquad 0 \leq r < n


The integer qq is the quotient — how many complete copies of nn fit inside aa. The integer rr is the remainder — what remains after those copies are removed. Modulo gives rr.

The expression 17mod5=217 \bmod 5 = 2 because 17=53+217 = 5 \cdot 3 + 2. Three complete groups of 55 account for 1515, leaving 22.

The expression 23mod7=223 \bmod 7 = 2 because 23=73+223 = 7 \cdot 3 + 2. Three complete groups of 77 account for 2121, leaving 22.

The expression 12mod4=012 \bmod 4 = 0 because 12=43+012 = 4 \cdot 3 + 0. The division is exact — nothing remains.

Notation

Mathematical notation writes the operation as amodna \bmod n. The expression is read "a modulo n" or simply "a mod n."

Most programming languages use the percent symbol instead: a%  na \% \; 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 amodna \bmod n denotes an operation that produces a number — a remainder. The expression ab(modn)a \equiv b \pmod{n}, 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=0a \bmod n = 0, the remainder vanishes — nn divides aa with nothing left over. In divisibility notation, nan \mid a. The statement 24mod6=024 \bmod 6 = 0 and the statement 6246 \mid 24 express identical facts in different languages.

When amodn0a \bmod n \neq 0, the remainder is positive — nn does not divide aa exactly. The statement 25mod6=125 \bmod 6 = 1 tells us 6256 \nmid 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 nn, the remainder amodna \bmod n is always one of the values {0,1,2,,n1}\{0, 1, 2, \ldots, n-1\}. There are exactly nn possibilities, and no remainder can reach nn itself — if it did, another complete copy of nn could be extracted.

When n=2n = 2, every integer produces a remainder of 00 or 11. This is the basis of even and odd: even numbers give 00, odd numbers give 11.

When n=10n = 10, every integer produces a remainder between 00 and 99. That remainder is the last digit of the number — 347mod10=7347 \bmod 10 = 7, read directly from the ones place.

When n=7n = 7, every integer maps to one of seven remainders: {0,1,2,3,4,5,6}\{0, 1, 2, 3, 4, 5, 6\}. No matter how large the dividend grows — thousands, millions, billions — the remainder when divided by 77 never escapes this set.

The modulo operation compresses the entire number line into a finite set of nn values, repeating in cycles. Every integer lands somewhere in {0,1,,n1}\{0, 1, \ldots, n-1\}, and infinitely many integers share each landing spot.

How to Compute a Remainder

To find amodna \bmod n, divide aa by nn, discard the fractional part of the quotient, multiply back, and subtract. The gap between the original number and the nearest multiple of nn below it is the remainder.

For 47mod547 \bmod 5: divide 47÷5=9.447 \div 5 = 9.4. The integer part is 99. Multiply back: 9×5=459 \times 5 = 45. Subtract: 4745=247 - 45 = 2. The remainder is 22.

For 123mod7123 \bmod 7: divide 123÷7=17.57123 \div 7 = 17.57\ldots. The integer part is 1717. Multiply back: 17×7=11917 \times 7 = 119. Subtract: 123119=4123 - 119 = 4. The remainder is 44.

The procedure has a compact formula using the floor function x\lfloor x \rfloor, which gives the greatest integer less than or equal to xx:

amodn=anana \bmod n = a - n \cdot \left\lfloor \frac{a}{n} \right\rfloor


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<na < n, the divisor exceeds the dividend and fits zero times. The entire value of aa is the remainder: 3mod10=33 \bmod 10 = 3, 0mod5=00 \bmod 5 = 0, 6mod7=66 \bmod 7 = 6. If 0a<n0 \leq a < n, then amodn=aa \bmod n = a — the number already lies within the remainder set and needs no reduction.

When n=1n = 1, every integer is a multiple of 11, so the remainder is always 00. The expression amod1=0a \bmod 1 = 0 for any integer aa. The remainder set {0,1,,n1}\{0, 1, \ldots, n-1\} collapses to {0}\{0\}.

When n=0n = 0, the operation is undefined. Division by zero has no meaning, and "the remainder when dividing by zero" inherits that meaninglessness. No quotient qq satisfies a=0q+ra = 0 \cdot q + r, because 0q=00 \cdot q = 0 regardless of qq. In programming, attempting a%  0a \% \; 0 raises a runtime error.

Negative Dividends

Everything above assumes aa 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(-7) \bmod 3 equals 1-1 under one convention and 22 under another. Both satisfy the division equation a=nq+ra = n \cdot q + r; they differ in how the quotient qq is rounded, which forces different values of rr.

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 nn, they are called congruent modulo nn. The notation is:

ab(modn)a \equiv b \pmod{n}


This reads "a is congruent to b modulo n" and means amodn=bmodna \bmod n = b \bmod n — both numbers land on the same remainder.

The expression 175(mod12)17 \equiv 5 \pmod{12} holds because 17mod12=517 \bmod 12 = 5 and 5mod12=55 \bmod 12 = 5. Both leave a remainder of 55 when divided by 1212.

The expression 232(mod7)23 \equiv 2 \pmod{7} holds because 23mod7=223 \bmod 7 = 2 and 2mod7=22 \bmod 7 = 2. Both leave a remainder of 22.

An equivalent way to test congruence: ab(modn)a \equiv b \pmod{n} if and only if n(ab)n \mid (a - b) — their difference is divisible by nn. For the first example, 175=1217 - 5 = 12, and 121212 \mid 12. For the second, 232=2123 - 2 = 21, and 7217 \mid 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 nn 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. aa(modn)a \equiv a \pmod{n} for any aa and nn, because aa=0a - a = 0 and n0n \mid 0.

Symmetric: if ab(modn)a \equiv b \pmod{n}, then ba(modn)b \equiv a \pmod{n}. If nn divides aba - b, it also divides bab - a.

Transitive: if ab(modn)a \equiv b \pmod{n} and bc(modn)b \equiv c \pmod{n}, then ac(modn)a \equiv c \pmod{n}. If nn divides both aba - b and bcb - c, it divides their sum (ab)+(bc)=ac(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=3n = 3, the integers split into three classes: those with remainder 00 (multiples of 33), those with remainder 11, and those with remainder 22. Every integer belongs to exactly one class.

The Clock Analogy

A 1212-hour clock is modular arithmetic made physical. After 1212 o'clock comes 11 o'clock, not 1313 — the numbers wrap around.

The time 15:0015{:}00 on a 2424-hour clock translates to 3:003{:}00 on a 1212-hour clock: 15mod12=315 \bmod 12 = 3. The time 2727 hours from midnight is 3:003{:}00 AM: 27mod12=327 \bmod 12 = 3. Different numbers, same position on the dial.

Days of the week follow the same pattern with n=7n = 7. Starting from Sunday (00), day 1010 of a sequence falls on the same weekday as day 33: 10mod7=310 \bmod 7 = 3, which is Wednesday. Day 1717 is also Wednesday. Day 2424 is Wednesday again. The cycle repeats every 77 days.

The general principle: any system that cycles with period nn is governed by modular arithmetic. Months cycle with period 1212. Musical notes (in Western chromatic tuning) cycle with period 1212. Degree measurements on a compass cycle with period 360360. 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\bmod 10. The last two digits are the number mod100\bmod 100. Identifying whether a number is even or odd is a mod2\bmod 2 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 99 if its digit sum is divisible by 99 rests on the fact that 101(mod9)10 \equiv 1 \pmod{9}. The rule for 1111 uses 101(mod11)10 \equiv -1 \pmod{11}. 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=0a \bmod n = 0 is the computational form of nan \mid 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 99, the last-digit test for 55, the alternating-sum test for 1111 — each one exploits a congruence property of 1010 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.