Back to Challenge Set 9

Code Challenge: Fizz Buzz

Difficulty  

divider

The Brief

What they asked for
A game we play on the bus. Count up from one. If the number
divides by three you say fizz instead of the number, if it
divides by five you say buzz, and if it divides by both you
say fizzbuzz. Print the first hundred so we can settle an
argument about who got 51 wrong.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • This is a counted loop with a decision inside it. You have had both since Activity 1.12, and the remainder operator since Activity 1.6. Nothing here is new.
  • The order of your checks decides whether this works. Work out why on paper before you type anything.
  • Fifteen is the number that catches people. Check it specifically, every time you change something.
  • A branch that can never be reached is not an error. The compiler will not warn you, the program will run, and the output will simply be wrong. That is the whole difficulty of this one.
  • There is no input at all. Read the brief again if that surprises you.

Sample Output

Sample Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16

If 15 prints as Fizz rather than FizzBuzz, your checks are in the wrong order — and nothing in your terminal will tell you so.

Settle the argument while you are here. Check what your program prints for 51, and be able to say why.


Submit

Your requirements list and your program file, named cc-fizz-buzz.

Commence Challenge