Formula Bro

Math and physics formulas, explained like you are new.


Modulo Operation

In one sentence: The modulo operation gives the leftover amount after you split one whole number into as many equal full groups of another number as you can.

amod⁡n=a−n⌊an⌋a \bmod n = a - n \left\lfloor \frac{a}{n} \right\rfloor

Read it out loud: “a mod n is the remainder when a is divided by n. It equals a minus n times the floor of a over n. The floor of a number means rounding it down to the nearest whole number, toward the left on the number line. The word mod is short for modulo, and it reads as the remainder.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Remainder after division

When you divide a whole number by another, you can fill as many full groups as possible, and what is left over is the remainder. For example, 17 divided by 5 gives 3 full groups of 5 with 2 left over. Written as a sentence, 17=3×5+217 = 3 \times 5 + 2, so the remainder is 2. The remainder is never as big as the group size.

Negative numbers

A negative number is less than zero. On a number line, −7-7 is seven steps to the left of 0. Subtracting a negative number is the same as adding a positive one, so 5−(−2)=75 - (-2) = 7. Negative numbers matter here because a remainder can come out differently when the number being divided is below zero.

The floor sign ⌊x⌋\lfloor x \rfloor

The floor of xx, written ⌊x⌋\lfloor x \rfloor, means round down to the nearest whole number, moving left on the number line. So ⌊5.71⌋=5\lfloor 5.71 \rfloor = 5, and ⌊−2.33⌋=−3\lfloor -2.33 \rfloor = -3, because −3-3 is the whole number directly to the left of −2.33-2.33. Rounding toward zero would give −2-2 instead, which is a different rule.

The word mod and amod⁡na \bmod n

The word mod is short for modulo, and it is written amod⁡na \bmod n. It reads ‘a mod n’ and means the remainder when aa is divided by nn. The number nn is the size of each group, so 17mod⁡5=217 \bmod 5 = 2 means that 17 split into groups of 5 leaves 2 over.

What each symbol means

SymbolMeaning

amod⁡na \bmod n

The modulo, the remainder after dividing aa by nn. It has no unit of its own, because it is a count of the leftover items, in the same units you are counting. For a positive nn, the answer is always a whole number from 0 up to n−1n - 1.

aa

The number being divided, the total we start with, such as 40 days. It can be positive or negative. Its unit is whatever you are counting, such as days, cookies or hours, and the remainder uses the same unit.

nn

The divisor, the size of each full group, such as 7 days in a week. It is a positive whole number. It has the same unit as aa, so if aa counts days, then nn also counts days, and the remainder counts days too.

⌊x⌋\left\lfloor x \right\rfloor

The floor of xx, the largest whole number that is less than or equal to xx. It rounds down, toward the left on the number line: the floor of 5.71 is 5, and the floor of −2.33-2.33 is −3-3. It has no unit, because the result is a whole count.

The formula piece by piece

an\frac{a}{n}

The plain division, 407≈5.71\frac{40}{7} \approx 5.71. It tells you how many groups of size nn fit into aa, counting a partial group as a fraction. Keep this number in mind, because the next piece removes the fraction.

⌊an⌋\left\lfloor \frac{a}{n} \right\rfloor

The number of full groups. Rounding the division down removes the partial group, so ⌊5.71⌋=5\lfloor 5.71 \rfloor = 5 full groups of 7 days. This is the whole-number part of the division, called the quotient.

n⌊an⌋n \left\lfloor \frac{a}{n} \right\rfloor

The total size of those full groups. Multiplying the group size by the number of full groups gives the amount of aa that the full groups use up. In the example, 7×5=357 \times 5 = 35 days are used by full weeks.

amod⁡n=a−n⌊an⌋a \bmod n = a - n \left\lfloor \frac{a}{n} \right\rfloor

The whole formula in one sentence: the remainder equals the number you started with, minus the total size of the full groups. Take the whole amount, subtract what the full groups use up, and what is left over is the remainder.

a≡r(mod⁡n)a \equiv r \pmod{n}

A standard textbook way to write the same idea, called congruence. It says that aa and rr leave the same remainder when divided by nn. Here rr is the remainder, so r=amod⁡nr = a \bmod n, and this notation is what mathematicians use when they work with remainders.

What the formula is doing, and why

The modulo operation answers one question: after you split aa into as many full groups of nn as possible, how much is left over? Seventeen cookies packed into bags of five make 3 full bags with 2 cookies left over, so 17mod⁡5=217 \bmod 5 = 2. The answer is a leftover count, a whole number, and it is never as big as the group size.

Why subtract? The formula is amod⁡n=a−n⌊a/n⌋a \bmod n = a - n \lfloor a/n \rfloor. The floor ⌊a/n⌋\lfloor a/n \rfloor counts the full groups. Multiplying that count by nn gives the total size of those groups, and subtracting it from aa leaves only the leftover. For a positive nn, the leftover always lies from 0 up to n−1n - 1, because one more full group would have fit otherwise.

Why round down, and why do negative numbers differ? Rounding down is the choice that makes the rule work for negative numbers too. Take −7mod⁡3-7 \bmod 3. The division −73≈−2.33\frac{-7}{3} \approx -2.33 rounds down to −3-3, so the answer is −7−3⋅(−3)=2-7 - 3 \cdot (-3) = 2. Python uses this floored rule. C, C++ and Java round toward zero instead, which gives −1-1 for the same expression, so check which rule your language uses before you rely on it.

Where it shows up. The modulo operation appears wherever a count wraps around, such as clock hours, weekdays and positions in a repeating list. It is also a key step in hashing and cryptography, where a huge number is reduced to a small range. It works on whole numbers, and its answers are whole numbers too.

An everyday picture

Think of a clock with 12 hours. If it is 10 o’clock and you wait 5 hours, the clock shows 3, not 15. The hour wraps back around after each full lap of 12, so the 15 becomes 3 after one lap is removed. The modulo is the number left after removing those full laps, so adding 5 hours to 10 o’clock gives (10+5)mod⁡12=3(10 + 5) \bmod 12 = 3.

Worked example: Days of the week and a negative remainder

Monday is day 0, Tuesday is day 1, and so on up to Sunday, which is day 6. We want the weekday 40 days after a Monday. That is the remainder of 40 divided by 7, since every 7 days returns us to the same weekday. We also check −7mod⁡3-7 \bmod 3 to see how negative numbers behave.

  1. Divide the days by the length of a week. A week has 7 days, so we divide 40 by 7: 407≈5.71\frac{40}{7} \approx 5.71. That means 5 full weeks and a bit more. The full weeks do not change the weekday, so only the part after them matters.
  2. Round down to count the full weeks. The floor of 5.71 is ⌊40/7⌋=5\lfloor 40/7 \rfloor = 5. Rounding down keeps the count of whole weeks and throws away the partial one. We will remove these 5 full weeks from the total in the next step.
  3. Find the leftover days. Remove the 5 full weeks from the 40 days: 40−7⋅5=40−35=540 - 7 \cdot 5 = 40 - 35 = 5. So 40mod⁡7=540 \bmod 7 = 5, which means 5 extra days after the full weeks are gone.
  4. Count those 5 days from Monday. Day 0 is Monday, so day 1 is Tuesday, day 2 is Wednesday, day 3 is Thursday, day 4 is Friday and day 5 is Saturday. The answer is Saturday, because the weekday number is always the remainder.
  5. Work through the negative case, −7mod⁡3-7 \bmod 3. We divide −73≈−2.33\frac{-7}{3} \approx -2.33. Rounding down gives −3-3, because −3-3 is the whole number to the left of −2.33-2.33 on the number line. Then −7−3⋅(−3)=−7+9=2-7 - 3 \cdot (-3) = -7 + 9 = 2. Subtracting a negative number is the same as adding it back, which is why the 9 is added.
  6. Compare with the rule that rounds toward zero. Rounding toward zero turns −2.33-2.33 into −2-2 instead. Then −7−3⋅(−2)=−7+6=−1-7 - 3 \cdot (-2) = -7 + 6 = -1. The same expression gives 2 in Python and −1-1 in C, which is why you must check the rule of your language before a program depends on it.
  7. Confirm with Python. The expression -7 % 3 in Python gives 2, which matches the floored rule. A program that prints −1-1 for the same expression is using rounding toward zero, so the two rules really do differ in practice.

Answer: 40 days after a Monday is a Saturday, since 40mod⁡7=540 \bmod 7 = 5. With the floored rule, −7mod⁡3=2-7 \bmod 3 = 2, which lies between 0 and n−1=2n - 1 = 2, as the rule promises.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

What is the difference between mod and remainder?

For positive numbers they match. For negative numbers the floored modulo always lands between 0 and n−1n - 1, while a truncated remainder can be negative, as in C and Java.

What is $a \bmod n$ when $a$ is a multiple of $n$?

It is 0. Then an\frac{a}{n} is a whole number, and subtracting nn times that number leaves nothing. For example, 35mod⁡7=035 \bmod 7 = 0, because 35 is exactly 5 full groups of 7.

Why does the formula need a floor?

The floor rounds the quotient down, and that choice decides what happens with negative numbers. The floor of −2.33-2.33 is −3-3, which makes the remainder come out as 2.

Sources

  1. Modulo, Wikipedia
  2. Floor and ceiling functions, Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/modulo-operation.md.