Home

Teaching Kids How to Code Using the Mod Operation

|
|  Updated:  
2018-06-29 16:20:37
Getting Started with Coding
Explore Book
Buy On Amazon
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.

About This Article

This article is from the book: 

About the book author:

Sarah Guthals, PhD, co-founded an ed-tech company and now continues to build technology for kids to learn, create, and share safely online. She loves to teach teachers how to teach coding in the classroom.

Camille McCue, PhD, is Director of Curriculum Innovations at the Adelson Educational Campus in Las Vegas where she leads the Startup Incubator, teaches STEM, and kickstarts K-12 learning initiatives.