# 03 Remainder
# Introduction to Remainder
The remainder operator is represented as the percent sign %. This just means the remainder after division. You may also hear this be called modulo. A common use for remainder is to test if a number is odd or even. To do this test, you divide the number by 2. If the remainder is zero, the number is even, but if the remainder is one, the number is odd. In code this would look like this:
int number = new Random().nextInt(100);
if (number % 2 == 0) {
System.out.println("number is even");
}
else {
System.out.println("number is odd");
}
Another use for remainder could be to track every 20 times a loop is executed, as follows:
for (int i=1; i<=1000; i++) {
// do some code
if (i % 20 == 0){
System.out.println("20 more repeats completed");
}
}
# Eclipse Recipes
FizzBuzz
# Processing Recipes
Bullseye Bumble Bee
# Bullseye
# Goal:
Use remainder to make a bullseye like the image above.
# Steps:
- This recipe practices using remainder to detect odd and even numbers.
- Find the Bullseye recipe program ( bullseye.pde ) and open it using Processing.
- Follow the instructions in the code to make a striped bullseye like the one in the picture.
- Make sure you SAVE YOUR CODE when you are done.
# Bumblebee
# Goal:
Draw the bumblebee using a for loop and remainder
# Steps:
- This recipe practices using remainder and calling methods that use parameters.
- Find the Bumble Bee recipe program ( bumble_bee.pde ) and open it using Processing.
- Follow the instructions in the code to make a long stripy bumble bee like the one in the picture.
- Make sure you SAVE YOUR CODE when you are done.
# Fizz Buzz
1, 2, fizz, 4, buzz, ...
# Goal:
Use a for loop and remainder (%) to build FizzBuzz - a children's counting game.
# Steps:
- Write a loop to print all the numbers between 1 and 20.
- Now modify the program so that any time a number is divisible by 3, print "fizz" in place of the number. And instead of printing numbers that are divisible by 5, print "buzz". For numbers that are divisible by both 3 and 5, print "fizzbuzz".
- If your code is correct, the program will now print:
1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz