Modulus (Modulo) Operator

In short: The modulus (modulo, or mod) operation returns the remainder after dividing one integer by another: 7 % 5 is 2. It's often used to keep values within a range, like wrapping an index around a circular array or checking whether a number is even.

The modulus operation (also called modulo or mod) finds the remainder after dividing one integer by another. It's usually written as %.

For example, \frac{7}{5} is 1 with a remainder of 2. So 7 % 5 is 2.

It's commonly used to force a number into a certain range. For example, any number, when modded by 3 gives us an integer in the range 0..2:

0 % 3 // 0 1 % 3 // 1 2 % 3 // 2 3 % 3 // 0 4 % 3 // 1 5 % 3 // 2 6 % 3 // 0 7 % 3 // 1

Frequently Asked Questions

What does the modulo operator do?

a % b gives the remainder when a is divided by b. For example, 10 % 3 is 1, because 10 = 3 x 3 + 1.

What is modulo used for in programming?

Wrapping indices in circular buffers, checking divisibility or even/odd (n % 2 == 0), hashing keys into a fixed number of buckets, and cycling through repeating patterns.

How does modulo work with negative numbers?

It depends on the language. In Python, -7 % 3 is 2 (the result takes the sign of the divisor); in Java, C, and JavaScript, -7 % 3 is -1 (the result takes the sign of the dividend).

Last updated: October 21, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .