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 an educator and author who loves mentoring students in all things STEM, computer science, and entrepreneurship. As the launching author of the For Kids For Dummies series, Camille has written over a dozen books with Wiley, all aimed at building confidence and capability in using technology. When not writing or teaching, Camille enjoys spending time with her family (including her fluff-tastic Corgis), attending comedy shows, and learning something new every day.