Fizz Buzz is a popular interview question to test the job candidates’ problem-solving ability. This tip shows how to solve Fizz Buzz in C# and C++ with six approaches.
This short write-up is a tip on solving Fizz Buzz. As such, it is not eligible to participate in the CodeProject monthly article competition.
Table of Contents
The example code is hosted on Github.
Introduction
Fizz Buzz is a popular interview question to test the job candidates’ problem-solving ability. The task is to write a program to count incrementally, replacing any number divisible by three with the word "Fizz"
, and any number divisible by five with the word "Buzz"
, and any number divisible by both three and five with the word "FizzBuzz"
.
The claim circulated on the web that most programmers cannot solve Fizz Buzz has been greatly exaggerated. We have no reason to believe that. But a junior programmer can be stumped by this question if he does not know that the division’s remainder can be used to find out if a number is multiple of the divisor. The answer is when the remainder is zero. In C# and C++, we use the modulus operator (represented by the percentage sign, %
) to calculate the remainder.
For example, we use division and remainder to find 146 minutes is how many hours and minutes. Below is the C# code. Please click on the C++ tab to see C++ code.
int totalMinutes = 146;
Console.WriteLine("{0} minutes is equal to {1} hours {2} minutes",
totalMinutes, totalMinutes / 60, totalMinutes % 60);
int totalMinutes = 146;
printf("%d minutes is equal to %d hours %d minutes",
totalMinutes, totalMinutes / 60, totalMinutes % 60);
The output is:
146 minutes is equal to 2 hours 26 minutes
As a small optimization tip, always do division and modulus operations in quick succession because the compiler can emit the same Intel x86 div instruction to calculate both division and remainder together. If there are many lines of code in between your division and remainder operation, the compiler may emit another div instruction for your remainder which is counter-productive.
The First Method
Please note the for-loop counter cannot start from zero because zero modulo by any number is zero. Both 0 % 3
and 0 % 5
give zero, and "FizzBuzz"
will be printed, which is not what we want because zero is not multiples of 3
and 5
.
static void FizzBuzz(int count)
{
for(int i = 1; i <= count; ++i)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
Console.WriteLine(i);
}
}
void FizzBuzz(int count)
{
for (int i = 1; i <= count; ++i)
{
if (i % 3 == 0 && i % 5 == 0)
{
std::cout << "FizzBuzz" << std::endl;
}
else if (i % 3 == 0)
{
std::cout << "Fizz" << std::endl;
}
else if (i % 5 == 0)
{
std::cout << "Buzz" << std::endl;
}
else
std::cout << i << std::endl;
}
}
The Optimized Method
As a minor optimization, since modulus is a time-consuming operation, we can cache the result in a variable to be reused.
static void FizzBuzzOptimized(int count)
{
for (int i = 1; i <= count; ++i)
{
bool multiplesOf3 = ((i % 3) == 0);
bool multiplesOf5 = ((i % 5) == 0);
if (multiplesOf3 && multiplesOf5)
{
Console.WriteLine("FizzBuzz");
}
else if (multiplesOf3)
{
Console.WriteLine("Fizz");
}
else if (multiplesOf5)
{
Console.WriteLine("Buzz");
}
else
Console.WriteLine(i);
}
}
void FizzBuzzOptimized(int count)
{
for (int i = 1; i <= count; ++i)
{
bool multiplesOf3 = ((i % 3) == 0);
bool multiplesOf5 = ((i % 5) == 0);
if (multiplesOf3 && multiplesOf5)
{
std::cout << "FizzBuzz" << std::endl;
}
else if (multiplesOf3)
{
std::cout << "Fizz" << std::endl;
}
else if (multiplesOf5)
{
std::cout << "Buzz" << std::endl;
}
else
std::cout << i << std::endl;
}
}
The Append Method
There is another way to do the Fizz Buzz program by concatenation of "Fizz"
and "Buzz"
whenever the number is both multiple of 3
and 5
without using WriteLine
or std::endl
in C++ case. The reason for not using WriteLine
is it sets the next output to the new line breaking up the words, "Fizz"
and "Buzz"
.
static void FizzBuzzAppend(int count)
{
for (int i = 1; i <= count; ++i)
{
bool output = false;
if (i % 3 == 0)
{
Console.Write("Fizz");
output = true;
}
if (i % 5 == 0)
{
Console.Write("Buzz");
output = true;
}
if (output == false)
Console.Write(i);
Console.Write('\n');
}
}
void FizzBuzzAppend(int count)
{
for (int i = 1; i <= count; ++i)
{
bool output = false;
if (i % 3 == 0)
{
std::cout << "Fizz";
output = true;
}
if (i % 5 == 0)
{
std::cout << "Buzz";
output = true;
}
if (output == false)
std::cout << i;
std::cout << '\n';
}
}
The Switch-Case Method
It is possible to do the Fizz Buzz program using switch-case. It is a variant of the optimized method. Submitted By Lewis1986
static void FizzBuzzSwitch(uint count)
{
for (uint i = 1; i <= count; ++i)
{
uint test = 0;
test |= (uint)((i % 3 == 0) ? 2 : 0);
test |= (uint)((i % 5 == 0) ? 4 : 0);
switch (test & 6)
{
case 6:
Console.WriteLine("FizzBuzz");
break;
case 2:
Console.WriteLine("Fizz");
break;
case 4:
Console.WriteLine("Buzz");
break;
default:
Console.WriteLine(i);
break;
}
}
}
void FizzBuzzSwitch(unsigned int count)
{
for (unsigned int i = 1; i <= count; ++i)
{
unsigned int test = 0;
test |= (i % 3 == 0) ? 2 : 0; test |= (i % 5 == 0) ? 4 : 0; switch (test & 6) {
case 6:
std::cout << "FizzBuzz\n";
break;
case 2:
std::cout << "Fizz\n";
break;
case 4:
std::cout << "Buzz\n";
break;
default:
std::cout << i << "\n";
}
}
}
The Addition Method
The method contributed By Jacques Fournier makes use of two more counters to keep track of whether they add up to 3
or 5
and reset them to zero. The subsequent checks detect the counter is zero to determine a multiple of 3
or 5
. It is an ingenious way to avoid using the time-consuming modulo operator.
static void FizzBuzzAddition(int count)
{
int c3 = 0;
int c5 = 0;
for (int i = 1; i <= count; ++i)
{
++c3;
++c5;
if (c3 == 3)
c3 = 0;
if (c5 == 5)
c5 = 0;
if (c3 == 0 && c5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (c3 == 0)
{
Console.WriteLine("Fizz");
}
else if (c5 == 0)
{
Console.WriteLine("Buzz");
}
else
Console.WriteLine(i);
}
}
void FizzBuzzAddition(int count)
{
int c3 = 0;
int c5 = 0;
for (int i = 1; i <= count; ++i)
{
++c3;
++c5;
if (c3 == 3)
c3 = 0;
if (c5 == 5)
c5 = 0;
if (c3 == 0 && c5 == 0)
{
std::cout << "FizzBuzz" << std::endl;
}
else if (c3 == 0)
{
std::cout << "Fizz" << std::endl;
}
else if (c5 == 0)
{
std::cout << "Buzz" << std::endl;
}
else
std::cout << i << std::endl;
}
}
The Optimized Addition Method
Bela Gyuricza slightly alters the above method by removing the checks to reset c3
and c5
to zero. The logic is hard to understand at first glance. I tested the code and it works correctly.
static void FizzBuzzAddition2(int count)
{
int c3 = 3;
int c5 = 5;
for (int i = 1; i <= count; ++i)
{
if (c3 == i && c5 == i)
{
Console.WriteLine("FizzBuzz");
c3 += 3;
c5 += 5;
}
else if (c3 == i)
{
Console.WriteLine("Fizz");
c3 += 3;
}
else if (c5 == i)
{
Console.WriteLine("Buzz");
c5 += 5;
}
else
Console.WriteLine(i);
}
}
void FizzBuzzAddition2(int count)
{
int c3 = 3;
int c5 = 5;
for (int i = 1; i <= count; ++i)
{
if (c3 == i && c5 == i)
{
std::cout << "FizzBuzz" << std::endl;
c3 += 3;
c5 += 5;
}
else if (c3 == i)
{
std::cout << "Fizz" << std::endl;
c3 += 3;
}
else if (c5 == i)
{
std::cout << "Buzz" << std::endl;
c5 += 5;
}
else
{
std::cout << i << std::endl;
}
}
}
The difference in solving style is a matter of preference.
Output
All six methods produce identical output.
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
History