Tuesday, February 1, 2011

Two C# Solutions for the FizzBuzz Problem

In case you are interested...

namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
const string fizz = "FIZZ";
const string buzz = "BUZZ";
const string fizzbuzz = "FIZZBUZZ";

// I did this solution second
for (int i = 1; i < 101; i++)
{
string output;
if (i / 15 * 15 == i) output = fizzbuzz;
else if (i / 3 * 3 == i) output = fizz;
else if (i / 5 * 5 == i) output = buzz;
else output = i.ToString();
Console.WriteLine(output);
}

// and this solution third
for (int i = 1; i < 101; i++)
{
string output = (i / 15 * 15 == i) ? fizzbuzz : (i / 3 * 3 == i)
? fizz : (i / 5 * 5 == i) ? buzz : i.ToString();
Console.WriteLine(output);
}

Console.ReadLine();
}
}
}
// Trust me, you don't want to see the one I did first.