Fizz Buzz

Put everything we've learned in this chapter to the test by creating a C++ version of the Fizz Buzz game.

Ryan McCombe
Updated

FizzBuzz is a word game used to teach young school children about division. The students sit in a circle and take turns counting, one number each.

Any number that is divisible by 3 is replaced with the word "Fizz"; numbers divisible by 5 are replaced with "Buzz", and numbers divisible by 15 are replaced with "FizzBuzz".

The sequence looks like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz,
11, Fizz, 13, 14, FizzBuzz, 16, 17...

This game, and variants of it, gained popularity within the programming community.

This is because implementing a program that can "solve" FizzBuzz requires a solid foundation in programming. It uses variables, operators, loops, conditional statements, and functions.

It is a useful milestone to ensure we understand all these basic principles. For this reason, candidates applying to programming jobs are sometimes asked to implement FizzBuzz in their interviews.

The Challenge

Our challenge is to do this in C++! We've learned all we need to implement FizzBuzz at this point. Using loops, functions, conditionals, and the modulus operator, we need to:

  • Write a program that logs out the numbers from 1 to 100.
  • Each number should appear in its own line
  • Numbers divisible by 3 are replaced with Fizz
  • Numbers divisible by 5 are replaced with Buzz
  • Numbers divisible by 15 are replaced with FizzBuzz
  • This should not require more than 30 lines of code

The output should look something like the following. I've omitted 17-97 for brevity, but the pattern continues in that range:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
  pattern continues until...
98
Fizz
Buzz

Starting Point

Remember to include the basic scaffolding we've been using so far, so we have access to the tools we need to log to the terminal.

A basic starting point is available below:

#include <iostream>
using namespace std;

int main() {

}

Hints

A series of hints, culminating in a full solution, are included below.

However, rather than immediately looking at the hints, consider reviewing the previous lessons, or searching the internet for assistance first.

This is very common even in professional settings. Much of programming involves figuring things out, so it's a good habit to practice.

When solving problems, try to not excessively rely on trial and error. Even if this approach ends up generating the desired output, if you don't understand why your code works, you can't transfer that success to other projects.

Remember the goal - it's not to create FizzBuzz - it is to learn how to program, so you can create whatever you want.

Summary

With any project, there are countless ways to write the underlying code, so your solution was almost certainly different to the example above.

  • Your variable and function names were probably different
  • You may have used more or fewer functions
  • Perhaps you used a different type of loop
  • The logic you used to decide what to output to the terminal may have been different

These are all valid options, so don't worry if your code looked completely different. If your program solved the problem and the logic defined in its code was easy to follow, it was a good solution.

Next Lesson
Lesson 20 of 60

Abstraction and Classes

Learn how to define, instantiate, and utilize classes, understanding how they form the backbone of object-oriented programming.

Have a question about this lesson?
Answers are generated by AI models and may not have been reviewed for accuracy