beanz Magazine


Anthony Albright on Flickr

Exploring another one of programmings many strange sounding but useful terms!

If you know a little bit about programming, there’s a good chance you’ve come across some of the weird terms that programmers like to use. While they have funny names, this doesn’t take away from the fact that they’re really important to learn what they mean and how they work!

Have you heard of the term “modulo”? It’s pronounced like “module” except you stick an O at the end of it. It sounds like some sort of supervillain, but it’s a lot more innocent than that!

In programming, modulo is represented by a percent symbol (%). You can use it like a mathematical symbol, such as + or -.

When you use modulo in an equation, modulo will divide the left number with the right one. However, instead of telling you the result of the division, modulo will instead give you the remainder of the division.

For example, If you told a computer to calculate 11 divided by 2 you’ll get 5.5. However, if you do 11%2 instead, the computer tell you what the remainder is. 2 goes into 11 five times, with a remainder of one; as such, 11%2 will equal 1.

It seems a little odd that programmers use modulo; after all, when do you ever want to know the remainder of a division? As it turns out, modulo is fantastic for telling us if one number can be divided evenly by another number.

Let’s imagine you calculate what 6%2 equals. When you press enter, the computer gives you a result of “0”. This means that there was no remainder left. Why? Easy: because 6 divides into 2 without any leftovers.

When this happens, we say that the number we’re diving up is “divisible” by the number we’re dividing with. In this case, 6 is divisible by 2, because 2 goes into 6 three times with no remainder.

Because of this, we can work out if one number is divisible by another using modulo. If modulo gives you a 0, it’s divisible; if it doesn’t, it’s not.

Now let’s put modulo to use. Do you know of the game “Fizzbuzz”? It’s a fun game where you and your friends take turns to count upward. When someone goes to say a number divisible by 3, they say “Fizz.” If it’s divisible by 5, they say “Buzz.” If it’s divisible by both 3 AND 5, they say “Fizzbuzz.” So, to start, it’d sound like this: 1, 2, Fizz, 4, Buzz, Fizz…

You can use modulo to make a computer program to play this game for you. You can set up a program that counts from 1 to 100, and checks each number using Modulo 3 and Modulo 5. If either (or both!) returns 0, you tell the computer to say “fizz,” “buzz,” or “fizzbuzz.”

Can you make a program that does the above? If you can, can you think of other ways you can put modulo to good use? Why not make an odd-even number detector, or a program where you enter two numbers and it tells you if one is divisible by another? The modulo sounds weird, but it’s a very useful tool!

Learn More

What Is Modulo?

C++ FizzBuzz With Modulo

Practical uses for the modulo operator

Modulo operation

What is modular arithmetic?

What does % mean in python?

Modulo operation facts

Fun with Modular arithmetic

Modular arithmetic

Modulo operator in Python

Also In The December 2020 Issue

Welcome to the far away land of the Ein Lifon where the number of appendages you have changes how you count!

You've looked at and used Susan Kare's work, or work inspired by hers, every time you use a computer.

Through the example of Theo Jansen and his Strandbeests we discuss the importance of fostering creativity and innovation in students.

From gopherspace to geminispace, the small internet is a lot bigger than you think!

Learn how asymmetric encryption and prime numbers help keep you safe online!

What could we accomplish if batteries lasted forever? These two companies want to find out!

Interesting stories about computer science, software programming, and technology for December 2020.