Skip to content

Repository files navigation

FizzBuzz kata in C++

CI C++17 License: MIT

Practice incremental design with the canonical or advanced FizzBuzz kata. Setup is complete when the starter test passes.

Overview

This kata complements Clean Code: Fundamentals, Ep. 3 - Functions.

From Wikipedia:

Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz," and any number divisible by five with the word "buzz."

Instructions

Canonical version

Write a program to print the first 100 FizzBuzz numbers, separated by newlines:

  • if a number is divisible by three, replace it with "Fizz";
  • if a number is divisible by five, replace it with "Buzz";
  • numbers divisible by both three and five become "FizzBuzz."

Sample output

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

Advanced version

Note

We recommend always starting with the canonical version, and only once you have it working proceed to the advanced version. This helps to model situations in production when we don't know all the requirements when a project starts.

Follow the same rules as the canonical version with these additional rules:

  • if a number is divisible by seven, replace it with "Fuzz";
  • if a number is divisible by 11, replace it with "Jazz";
  • if a number is divisible by any combination of 3, 5, 7, or 11, replace it with concatenated values corresponding to each divisor: e.g., 55 becomes "BuzzJazz."

Sample output

...
86
Fizz
Jazz
89
FizzBuzz
Fuzz
92
Fizz
94
Buzz
Fizz
97
Fuzz
FizzJazz
Buzz

Prerequisites

Required:

Optional:

  • GNU Make, for shorter commands. Every required task also has direct CMake and CTest commands. Make may be unavailable on Windows.

You do not need to install GoogleTest separately. CMake finds an installed copy or downloads the pinned release when needed.

Set up the kata

  1. Clone the repository:

    git clone https://github.com/Coding-Cuddles/fizzbuzz-cpp-kata.git
  2. Enter the repository directory:

    cd fizzbuzz-cpp-kata
  3. Run the starter test. Use Make when it is installed:

    make test

    Otherwise, use CMake and CTest directly:

    cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
    cmake --build build --config Debug
    ctest --test-dir build --build-config Debug --output-on-failure

The first run may download and build GoogleTest. CTest should report 100% tests passed.

If a command reports a missing compiler or CMake, install that prerequisite and run the setup commands again.

Setup is complete when CTest reports 100% tests passed.

Work on the kata

  1. Replace the starter assertion in test_fizzbuzz.cpp with the first kata test.

  2. Run the tests after each change. Use Make when it is installed:

    make test

    Otherwise, use CMake and CTest directly:

    cmake --build build --config Debug
    ctest --test-dir build --build-config Debug --output-on-failure

    Continue when CTest reports 100% tests passed.

Make command reference

Make is optional. Run make or make help to list these commands in the terminal.

Command Result
make all Build and run the test suite
make help Show the Make command reference
make build Configure and build without running tests
make test Build and run the test suite
make format Format tracked C++ and header files
make format-check Check formatting without changing files
make clean Remove generated build artifacts

About

FizzBuzz kata in C++

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Used by

Contributors

Languages