Skip to content

Instantly share code, notes, and snippets.

@dialalpha
Forked from Pharylon/gist:4502183
Last active December 10, 2015 22:38
Show Gist options
  • Save dialalpha/4504024 to your computer and use it in GitHub Desktop.
Save dialalpha/4504024 to your computer and use it in GitHub Desktop.
FizzBuzz. Can be written in shorter lines of code. But I find this implementation to be clearer.
using System;
namespace Dialalpha.FizzBuzz
{
class Program
{
static void Main(string[] args)
{
bool fizz = false, buzz = false;
for (int i = 1; i <= 100; ++i)
{
fizz = (i % 3 == 0); // true if i is a multiple of 3
buzz = (i % 5 == 0); // true if i is a multiple of 5
if (fizz && buzz)
Console.WriteLine("FizzBuzz");
else if (fizz)
Console.WriteLine("Fizz");
else if (buzz)
Console.WriteLine("Buzz");
else Console.WriteLine(i);
}
}
}
}
@EricBeales
Copy link

I think this is the cleanest and probably most efficient solution, but if I am going to be a stickler:

  • 'fizz' and 'buzz' don't need assignments (the "= false") at the beginning
  • For consistency "Console.WriteLine(i);" should go on the next line
  • Some comments would be nice, specifically something saying that "(i % 3 == 0)" is done to check if it is a multiple of 3. (a lot of new coders haven't really used the modulus operator so they would have no idea what you are doing or why there)

@dialalpha
Copy link
Author

@EricBeales: I usually tend to initialize my booleans when I declare them just to be on the safe side. I'll edit to add some comments though. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment