Surprisingly, I had been asked to implement this simple algorithm twice among my 3 interviews.
Problem:
- Loop from 1 to 100
- If value is divided by 3, print Fizz
- If value is divided by 5, print Buzz
- If value is divided by 3 & 5, print Fizz Buzz
- Else print the value
Solution: This problem is designed to test how your code look like. Is it short and clean, not too many condition statement.
C#
private static void FizzBuzz(int value)
{
string result;
for (int i = 1; i <= value; i++)
{
result = null;
if (i % 3 == 0)
result = "Fizz";
if (i % 5 == 0)
result += "Buzz";
Console.WriteLine(result ?? i.ToString());
}
}
0 comments:
Post a Comment