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:
Interview coming up?
Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can break down tricky coding interview questions.
No prior computer science training necessary—we'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe whenever.
You're in! Head over to your email inbox right now to read day one!