FizzBuzz Test

I've seen the FizzBuzz test come up at a couple of programming competitions, and I hear that it is an interview favorite amongst some employers. The test is easy, and allows an employer to see your coding style. While no style is shared by all programmers it is important to see if the candidate can at-least remain consistent.

So what is the FizzBuzz test?

The test can come in a multitude of varieties, the most common that I have seen is described as:
A program that counts from 1 to 100. When a number is divisible by 3, only print 'Fizz.' If the number is divisible by 5, only print 'Buzz.' If the number is divisible by 3 and 5, print 'FizzBuzz.' If the number is neither of the aforementioned cases, print the number.

It may seem somewhat difficult if you're not familiar with the modulus operator (%), so we'll get everyone caught up to speed. If we use the code cout << 9 % 2 << endl the console will output 1.This is because when we divided the integer 9, by 2, we get a remainder of 1.

We use the modulus operator in the FizzBuzz test to tell if the 3, and/or the 5, fit evenly into a given number by ensuring there isn't a remainder.

if (0 == 6 % 3) will evaluate to TRUE, as 6 is evenly divided by 3.

Now that the modulus operator is in our tool box we can start laying out what we need to do. Looking at the description, we can tell that the first thing that is necessary is a basic loop. The loop will run from 1 to 100, and we will need to check every single number for divisions of 3 and 5.

#include <iostream>
using namespace std;
int main()
{
    cout << "[START]" << endl;
    for(int i = 1; i <= 100; i++)
    {
            cout << i;

            if( 0 == i%3)
                    cout << "Divisible by 3!";
            if( 0 == i%5)
                    cout << "Divisible by 5!";
            cout << endl;
    }
    return 0;
}

Seems like we're almost done already, doesn't it ? Thats because we almost are. What needs to be done now is changing our output statements to their respective 'Fizz' or 'Buzz,' and handle the combined case of 'FizzBuzz.'

#include <iostream>
using namespace std;
int main()
{
    cout << "[START]" << endl;
    for(int i = 1; i <= 100; i++)
    {
            // If i is evenly divisible by 3
            if( 0 == i%3)
            { 
                 /*
                       Check if it is 3 and 5, 
                       or if it is just 3.
                 */
                 if( 0 == i%5)
                     cout << "FizzBuzz";
                 else
                    cout << "Fizz";
            }
            else if( 0 == i%5)
            {
                 /*
                       We chain the previous if with 
                       an else-if to ensure we don't 
                       print something silly like 
                       'FizzBuzzBuzz.'
                 */ 
                 cout << "Buzz";
            }
            else
            {
                 /*
                       Finally, if it isn't divisible by 3, 
                       and/or 5, we print the integer.
                 */ 
                    cout << i;
            }
            // End the line for readability 
            cout << endl;
    }
    return 0;
}

The first few lines of our output from the above code will be:

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

It really is interesting to see the different ways people choose to accomplish this FizzBuzz test. Sometimes they choose to clean-up the output to something different, and sometimes they function-out the modulus operations (I don't really know why though.) The important thing to take out of how someone completes the FizzBuzz test is their understanding of controlling program flow.

I really enjoyed doing the FizzBuzz test when I first heard about it, and once I completed it, I re-wrote it a few times to see what other ways it could be solved.

Here is a slightly different solution, that uses an extra modulus to format the output :

#include <iostream>

using namespace std;

int main()
{
    cout << "[START]" << endl;

    unsigned f = 1;
    for(int i = 1; i <= 100; i++)
    {
        if(0 == i%3)
        {
                if(0 == i%5)
                        cout << "Fizz-Buzz, ";
                else
                        cout << "Fizz, ";
                f = 0;
        }
        else if(0 == i%5)
        {
                cout << "Buzz, ";
                f = 0;
        }

        if(0 == i % 10)
                cout << endl;
        if( f )
                cout << i << ", ";
        f = 1;
    }
    cout << "[END]" << endl;

    return 0;
} 

Console Output :

[START]
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 
11, Fizz, 13, 14, Fizz-Buzz, 16, 17, Fizz, 19, Buzz, 
Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz-Buzz, 
31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 
41, Fizz, 43, 44, Fizz-Buzz, 46, 47, Fizz, 49, Buzz, 
Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, Fizz-Buzz, 
61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 
71, Fizz, 73, 74, Fizz-Buzz, 76, 77, Fizz, 79, Buzz, 
Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, Fizz-Buzz, 
91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz, 
[END]

I encourage you to look for other ways to do the FizzBuzz test, and to look for any alterations to the rules that could further demonstrate the concept of control flow!