###
What is the Modulus/mod Operator?

What is the Modulus/mod Operator?

When you are programming, you have the standard operations addition, subtraction, multiplication, and division. You also have another operator which you may not be familiar with. This is the modulus (mod) operator.

The modulus operator finds remainders. Allow me to illustrate this with an example.

**5/3**

3 cannot divide into 5 evenly, so after taking the closest multiple of three which is below 5 and subtracting it, we are given our remainder. The remainder in our case was 2. Often you will see it written as (5)mod(3).

What can you use the modulus operator for? One of its most simplistic uses is to distinguish even and odd numbers. All even numbers divide by 2 evenly. They don't have any remainders. This means that if (*number*)mod(2) = 0, then it is even. Otherwise it is odd. Below is a program which utilizes this concept to tell you if a number is even or odd.

This was a quick introduction to the mod(modulus) operator.

Press the green flag to run the program!

## No comments:

## Post a Comment