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.
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:
Fizz
Buzz
FizzBuzz
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
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() {
}
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.
We can break down the task into steps, using comments to lay out our plan initially.
We can also plan what functions we might want to create, as well as their arguments and return type. We can add their basic structure to our code.
That might look something like this:
#include <iostream>
using namespace std;
void Log(int Number) {
// If Number is divisible by 15, log FizzBuzz
// Else If Number is divisible by 5, log Buzz
// Else If Number is divisible by 3, log Fizz
// Else, log out the number
// Log out a '\n'
}
int main() {
// Call Log 100 times, with the numbers from
// 1 to 100
}
We can use the modulus oeprator, %
, to determine if a number is divisible by another number.
I've added more details to the comments, and started writing the body of the Log
function.
#include <iostream>
using namespace std;
void Log(int Number) {
// If Number is divisible by 15, log FizzBuzz
if (Number % 15 == 0) {
cout << "FizzBuzz";
}
// else if Number is divisible by 5, log Buzz
// else if Number is divisible by 3, log Fizz
// else, log out the number
// Log out a '\n'
}
int main() {
// Using a loop, eg a while loop, call Log
// 100 times with the numbers from 1 to 30
}
Lets fill out most of our logic. I've left a few comments for some remaining steps.
#include <iostream>
using namespace std;
void Log(int Number) {
if (Number % 15 == 0) {
cout << "FizzBuzz";
} else if (Number % 5 == 0) {
cout << "Buzz";
} else if (/* Is number divisible by 3? */) {
cout << "Fizz";
} else {
// Log out the number
}
// log out a '\n'
}
int main() {
int LoopIteration{1};
while (/* loop 100 times */) {
++LoopIteration;
// call the Log function
}
}
There are many possible ways to create a solution to any coding problem.
One possible solution might look like this:
#include <iostream>
using namespace std;
void Log(int Number) {
if (Number % 15 == 0) {
cout << "FizzBuzz";
} else if (Number % 5 == 0) {
cout << "Buzz";
} else if (Number % 3 == 0) {
cout << "Fizz";
} else {
cout << Number;
}
cout << '\n';
}
int main() {
int LoopIteration{1};
while (LoopIteration <= 100) {
Log(LoopIteration);
++LoopIteration;
}
}
Put everything we've learned in this chapter to the test by creating a C++ version of the Fizz Buzz game.
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way