Fizz Buzz

This is a Rosetta Code post.

Story

A popular interview question is the FizzBuzz test, its based on a children’s game where you count from 1 to 100 and for multiples of 3 say ‘Fizz’, multiples of 5 say “Buzz”, if both say ‘FizzBuzz’ else say the number.

For example, a typical round of fizz buzz would start as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
Fizz
4
Buzz Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
...

Task

Write a program that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz.

Solutions

Recursive

TODO!

Iterative

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void GetFizzBuzz()
{
var outPut = "";

for (int i = 0; i <= 100; i++)
{
outPut = "";

if (i % 3 == 0)
outPut = "Fizz";

if (i % 5 == 0)
outPut += "Buzz";

if (outPut.Equals(""))
outPut = i.ToString();

Console.WriteLine(outPut);
}
}

References