Instead of delving straight into the challenge question, we are going to first start with a brief lesson. Today we are going to learn about modulus. Modulus is defined as "a quantity by which two given quantities can be divided to yield the same remainders." but what does that mean to us as programmers?
To help you best understand, let's zip back in time to grade school when you first learned to do division. When you were asked to do 4 divided by 3 you did not come up with a decimal, the first thing you were taught is that the answer is 1 remainder 1. Well that is exactly what modulus is, it is the remainder after a number is divided. So, as an example, 15 modulo 3 would return 0 because, there is no remainder, the answer to the problem 15/3 is 5 remainder 0. And if the question was 20 modulo 3 then it would return 2, because 20/3 is 6 remainder 2. If the number you are dividing is negative, then the result will be negative too. So for example -10 modulo 3 would return -1 because the remainder of 10/3 is 1 and the dividend (-10) is negative.
The way that we do this calculation in PHP is by using a percent sign. Here are some examples below:
<?php
echo (5 % 3)."\n"; // the remainder is 2
echo (10 % 3)."\n"; // the remainder is 1
echo (-5 % 3)."\n"; // the remainder is -2
echo (-10 % 3)."\n"; // the remainder is -1
?>Now let's apply this information with our challenge question!
Challenge Question:
Create a program that will print out all factors of 7 between 1 and 1000. Each number should be on a separate line. The program should calculate the numbers, manually figuring out all the factors and printing them back does not count! Hint: use modulus!
If you would like to share your answer to this question, you can do so in the related blog post, or in the About PHP forum. There is more than one way to answer the challenge question!
Do not read the answer below until you have attempted to do the problem yourself!
My Answer:
<?php
$i = 1;
while ( $i <=1000 )
{
$m = ($i % 7);
if ($m==0)
{
echo $i."<br>";
}
$i++;
}
?> Let's take a look at the solution above.
The first thing you will see is that we are assigning $i to 1 (because we are using numbers 1 to 1000) and then putting it into a while loop, which will continue to cycle through numbers one at a time as long as $i is not over 1000. The code at the bottom that says $i++ is saying that you should add 1 to $i, and go back to the top of the loop.
Once we are in the loop we use modulus on each number to divide it by 7. If the number is a factor of 7, then there will be no remainder, so we are only echoing the numbers who have a 0 remainder. That is done with the variable $m which is first set to the remainder, and then run through an if statement to see if the number should be echoed. We also include a break (<br>) to make sure each number is on a fresh line.
Modulus or Modulo is used in a lot of other programming languages as well. In many scenarios knowing the remainder can be just as if not more important than knowing the answer to a problem. Here is some information on using modulo in Javascript.

