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, Fizz Buzz, 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 interview.
Our challenge is to do this in C++! We've learnt 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 coninues 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
Use the following code as a starting point:
#include <iostream>
using namespace std;
int main() {
}
A series of hints, culminating in a full solution, are included with challenges. However, I'd recommend trying the challenges yourself before looking at the hints. All the tools needed to complete a challenge were provided in the proceeding lessons.
Additionally, if you get stuck, rather than looking at a hint, considering searching the internet for assistance first. This is a very common practice even in professional programming.
When getting help on the internet, you are likely to find code you don't understand. I'd caution against just copying and pasting in code. Even if it ends up working, you haven't really learnt anything if you don't understand why it is working.
The bigger goal is not to create FizzBuzz - it is to learn how to program, so you can create whatever you want eventually.
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 out FizzBuzz
// Else If Number is divisible by 5, log out Buzz
// Else If Number is divisible by 3, log out Fizz
// Else, log out the number
// Log out an endl
}
int main() {
// Call Log 100 times, with the numbers from 1 to 100
}
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 out FizzBuzz
if (Number % 15 == 0) {
cout << "FizzBuzz";
}
// else if Number is divisible by 5, log out Buzz
// else if Number is divisible by 3, log out Fizz
// else, log out the number
// Log out an endl
}
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 more TODO
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 (/* TODO: Check if number divisible by 3 */) {
cout << "Fizz";
} else {
// TODO: Log out the number
}
// TODO: log out an endl
}
int main() {
int LoopIteration { 1 };
while (/* TODO: boolean expression to loop 100 times */) {
LoopIteration++
// TODO: call Log function
}
}
There are many possible ways to code any challenge - 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 << endl;
}
int main() {
int LoopIteration { 1 };
while (LoopIteration <= 100) {
Log(LoopIteration);
LoopIteration++;
}
}
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way