Teaching Kids How to Code Using the Mod Operation - dummies

Teaching Kids How to Code Using the Mod Operation

By Camille McCue, Sarah Guthals

One useful operator that can be tricky for young coders at first is modulo, also referred to as modulus and mod. The mod of two numbers is the remainder after a division of two numbers.

This operator can be useful for writing applications based on real-world scenarios. For example, say you have $20.00. You want to pass out exactly $3.50 to as many friends as possible, and you want to know how much money you will have leftover. So you would want to solve the questions “What is the quotient of 20.00/3.50?” and “What is the remainder of 20.00/3.50?” The answer to this word question would be “You can give 5 friends $3.50 and you will be left with $2.50.”

What is really great is that Google provides a mod function on its online calculator! So if you and your coder are ever debugging, you can check your mod equation on Google fairly easily. Just go to google.com and type “number1 % number2” and the calculator appears with your answer. There is even a button for mod on the calculator; it’s the % symbol.

Using pseudocode

Mod in pseudocode is often represented with the % symbol, because that is most commonly used in text-based languages like Python and Java.

number1 % number2

Examples are

30 % 7
6.89 % 5.9

Using Scratch

In Scratch, you can find a mod block under the Operators category. For example, this image shows the mod block evaluating 9 mod 8, which equals 1.

mod block coding
The mod block found in the Scratch Operators category.

Using Python

In Python, the mod operator is very similar to the basic math operations such as adding, subtracting, multiplying, and dividing. You simply use the % symbol between the numbers. It’s exactly like the pseudocode examples for both integers and decimals.