Creating FizzBuzz in C++

An introduction to Challenges, and the first one - creating a C++ version of the FizzBuzz game.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Teacher working in a classroom
Ryan McCombe
Ryan McCombe
Posted

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.

The Challenge

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:

  • 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 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

Starting Point

Use the following code as a starting point:

#include <iostream>
using namespace std;

int main() {

}

Hints

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++;
  }
}

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

What is a Class in C++

In this lesson, we will introduce classes - the fundamental feature of object oriented programming
3D art showing a character looking into the distance
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved