Mathematics with whole numbers behaves differently - when dividing numbers that aren’t even multiples, there’s always some amount left over. It may be helpful to think back to your early math lessons, before you learned fractions and decimals. 5 divided by 5 equals 1, with a remainder of 0 5 divided by 4 equals 1, with a remainder of 1 5 divided by 3 equals 1, with a remainder of 2 5 divided by 2 equals 2, with a remainder of 1 5 divided by 1 equals 5, with a remainder of 0
Some examples may help illustrate this, as it’s not necessarily intuitive the first time you encounter it: 5 % 1 = 0 Instead of returning the result of the division, the modulo operation returns the whole number remainder. The modulus operator - or more precisely, the modulo operation - is a way to determine the remainder of a division operation.
You next response, understandably, might be, “That doesn’t clarify anything,” so let’s take a closer look: The modulus operator, written in most programming languages as % or mod, performs what is known as the modulo operation. I was not taught %, the modulus operator, which I recently discovered can be quite useful and interesting in its own right. These are the four mathematical operations I was taught during my childhood education, and their operators, +, -, *, /, are very familiar. Addition, subtraction, multiplication, and division.