Programming

Modulo Operator Explained: What Is Modulo and How Mod Works

The modulo operator returns the remainder of a division. Learn what modulo means, how the mod operator works in code, its precedence, negative-number behaviour and real uses in one guide.

By Remainder Calculator Team

Divide one whole number by another and you’re often left with a scrap that won’t fit evenly. That scrap is what the modulo operator hands back. The expression a % b gives the remainder of a divided by b, so 17 % 5 returns 2 because 5 fits into 17 three times with 2 left over. Most languages spell it with a percent sign, and it ranks right next to addition and multiplication among the arithmetic operators you reach for every day. Feed any two numbers into the Remainder Calculator and you’ll get the same value this guide walks through, from what the operator does to how it behaves and where it turns up.

Three things make it worth knowing. It wraps numbers into a fixed range, it checks divisibility in a single step, and it pulls repeating cycles out of sequences that would otherwise grow without limit. That’s why you find modulo behind clock arithmetic, hashing, cryptography, calendar math, and any loop built to repeat on a fixed period.

Every modulo expression breaks into 3 parts: the dividend on the left, the operator in the middle, and the divisor on the right, which also goes by the name modulus. From here the guide covers what modulo means, how the mod operator runs step by step, its precedence, the split that pulls two languages apart on negative numbers, and the jobs it does in real code.

What Is Modulo?

Modulo (mod) is the operation that finds the remainder of Euclidean division. The result of a mod b is the amount left over after removing every whole copy of b from a.

Computing 17 mod 5 removes 3 copies of 5, which accounts for 15, and leaves 2. The value 2 is the modulo. Written in full, 17 = 5 × 3 + 2, where 3 is the quotient and 2 is the modulo result.

The modulus b sets the range of every possible answer. A modulus of 5 produces exactly 5 results, 0 through 4, and no output ever reaches 5. This bounded range is the defining feature of modulo:

  • The result is greater than or equal to 0 for positive inputs.
  • The result is strictly smaller than the modulus.
  • The result is always a whole number.

Modulo and remainder describe the same operation for positive numbers, and they split apart for negative ones. The exact boundary is covered in why modulo and remainder are not identical, and the by-hand calculation behind every modulo result is the manual method behind the operator.

What Is the Modulo Operator?

The modulo operator is the symbol that tells a programming language to compute a modulo. Most languages use the percent sign, written between the two operands as a % b.

The operator name changes across contexts while the operation stays the same:

  • In C, C++, Java, JavaScript, PHP and Go, the operator is %.
  • In Python, % is modulo and divmod(a, b) returns the quotient and modulo together.
  • In Excel and Google Sheets, the function is MOD(a, b).
  • In Visual Basic and Pascal, the keyword is Mod.
  • In mathematics, the written form is a mod b or the congruence a ≡ r (mod b).

Inside a modulo expression the percent sign has nothing to do with percentages. 50 % 7 asks for the remainder of 50 divided by 7, which is 1. It isn’t a fraction of 50.

How the Mod Operator Works

The mod operator works in 3 steps: divide the dividend by the divisor, take the whole-number quotient, then subtract the divisor times that quotient from the dividend.

Evaluate 47 % 6:

  1. Divide. 47 ÷ 6 = 7.83, so the whole-number quotient is 7.
  2. Multiply. 6 × 7 = 42.
  3. Subtract. 47 − 42 = 5.

The operator returns 5. The same formula, a % b = a − b × floor(a / b), produces every modulo result, and it matches the remainder formula because modulo and remainder share the definition of Euclidean division. Working the same division out by hand follows the full long division method.

A dividend smaller than the divisor returns the dividend unchanged. 3 % 8 returns 3, since 8 fits into 3 zero times and all of 3 is left over. A dividend that is an exact multiple returns 0. 24 % 6 returns 0, which confirms that 6 divides 24 evenly, the case explained in what a remainder of zero means. For common divisors, faster mental shortcuts return the same value without any division.

Modulo Operator Precedence

The modulo operator shares precedence with multiplication and division, and all 3 evaluate left to right before addition and subtraction.

The expression 10 + 20 % 7 evaluates the modulo first, so 20 % 7 returns 6 and the result is 16, not 30. Reading it as (10 + 20) % 7 would give 2, which is wrong.

Three precedence rules prevent modulo bugs:

  • Modulo binds tighter than + and , so a + b % c means a + (b % c).
  • Modulo binds equal to * and /, so a * b % c evaluates left to right as (a * b) % c.
  • Parentheses override every default, so write (a + b) % c when the sum must be reduced.

Wrap the operands you mean in parentheses and the ambiguity is gone. It costs nothing at runtime.

Modulo With Negative Numbers

The modulo of a negative number depends on the language, because two definitions disagree on the sign of the result. The expression -7 % 3 returns 2 in Python and −1 in C, C++, Java and JavaScript.

The split comes from how each language rounds the quotient:

  • Floored division rounds the quotient toward negative infinity, so Python computes floor(−7 / 3) = −3, then −7 − 3 × (−3) = 2. The result takes the sign of the divisor.
  • Truncated division rounds the quotient toward zero, so C computes trunc(−7 / 3) = −2, then −7 − 3 × (−2) = −1. The result takes the sign of the dividend.

Forcing a non-negative result in a truncating language uses the expression ((a % b) + b) % b, which converts −1 into 2. This single line prevents the off-by-one bugs that appear when negative indices wrap through a modulo. The full breakdown of every sign case sits in the remainder of negative numbers, and the language-by-language behaviour sits in modulo in Python, JavaScript and C.

What Is Modulo Used For?

Modulo powers 6 common tasks that all rely on wrapping a number into a fixed range:

  • Even and odd tests. n % 2 returns 0 for even numbers and 1 for odd numbers, the fastest parity check in code.
  • Divisibility tests. n % d == 0 confirms that d divides n exactly, which is using modulo to test divisibility inside a program.
  • Clock and calendar math. Adding 25 hours to 10 o’clock uses (10 + 25) % 12, which returns 11. Day-of-week calculations use % 7.
  • Cycling through a list. index % length keeps an array index inside bounds, so a carousel returns to the first slide after the last one.
  • Hashing. A hash table maps any key into a bucket with hash % table_size, which is why table sizes are chosen with care.
  • Cryptography. The RSA algorithm encrypts data through modular exponentiation, where every multiplication is followed by a modulo to keep the numbers bounded.

Each use exploits the same property: modulo collapses an unbounded input into a small, predictable set of outputs.

Modulo in Clock Arithmetic

Clock arithmetic is the everyday model of modulo, where the modulus is 12 for hours or 60 for minutes. A 12-hour clock never shows 13, because 13 mod 12 is 1.

Find the time 50 hours after 9 o’clock: (9 + 50) % 12 reduces 59 mod 12 to 11, so the clock reads 11 o’clock. Modulo discards every full 12-hour cycle and keeps only the position within the current cycle. This is the same wrapping behaviour that a program uses to loop an animation or reset a counter.

Modulo and Divisibility

Modulo answers whether one number divides another in a single comparison. The test n % d == 0 returns true when d divides n with no remainder, which makes d a factor of n.

The check 100 % 4 == 0 is true, so 4 is a factor of 100. The check 100 % 7 == 0 is false, since 100 % 7 returns 2, so 7 is not a factor. This one-line test replaces every divisibility rule inside code and drives primality checks, where a number is prime when no divisor from 2 to its square root returns a modulo of 0.

Modulo Worked Examples

ExpressionQuotientResult
17 % 532
47 % 675
100 % 7142
24 % 640
3 % 803
-7 % 3 (Python)−32
-7 % 3 (C)−2−1

Common Modulo Mistakes

Four errors account for most modulo bugs:

  • Assuming -7 % 3 returns 2 in every language, when C and JavaScript return −1.
  • Forgetting operator precedence, so a + b % c is read as (a + b) % c.
  • Using modulo by 0, which raises a ZeroDivisionError in Python and is undefined behaviour in C.
  • Treating the percent sign as a percentage instead of a remainder.

Modulo by 0 has no defined result, because division by 0 has no quotient. Guard the divisor before every modulo when its value is not fixed.

Frequently Asked Questions

What does the modulo operator do?

The modulo operator returns the remainder of a division. The expression a % b divides a by b and gives back only the leftover, so 17 % 5 returns 2.

Is modulo the same as remainder?

Modulo and remainder give the same result for positive numbers and differ for negative ones. Python’s -7 % 3 returns 2, while C’s -7 % 3 returns −1, because the two use different rounding for the quotient.

Why does the modulo operator use the percent sign?

The percent sign was chosen as the modulo symbol in the C language and inherited by C++, Java, JavaScript and PHP. It has no link to percentages inside a modulo expression.

What happens with modulo by zero?

Modulo by zero is undefined. Python raises a ZeroDivisionError, JavaScript returns NaN, and C produces undefined behaviour, so the divisor must be checked before the operation.

What is n % 2 used for?

The expression n % 2 tests whether a number is even or odd. It returns 0 for even numbers and 1 for odd numbers, the fastest parity check available in code.

Conclusion

It all comes down to one idea. a % b hands back what’s left after the division, whether you write it that way in code or as MOD(a, b) in a spreadsheet. Divide, keep the whole-number quotient, subtract the divisor times that quotient, and the answer always lands between 0 and one less than the modulus.

That bounded range is what makes modulo pay off. It wraps numbers into a set span. It flags whether one number divides another. And it shrinks runaway sequences into short cycles. Those three jobs are why the same operator turns up inside clock arithmetic, hashing, and the RSA algorithm. Negative numbers are the one spot to stay alert, since Python and C disagree on the sign of the result. When you want to check a modulo answer against what your code returns, drop the dividend and divisor into the Remainder Calculator.

← Back to Blog