Skip to content

Instantly share code, notes, and snippets.

@maads
Last active August 29, 2015 14:13
Show Gist options
  • Save maads/10e804bcd2e2341b8e64 to your computer and use it in GitHub Desktop.
Save maads/10e804bcd2e2341b8e64 to your computer and use it in GitHub Desktop.
FizzBuzz division
// FizzBuzz without modulo
public class Program
{
public static void Main()
{
for (int i = 1; i <= 100; i++)
{
Console.WriteLine(FizzBuzz(i));
}
Console.ReadKey();
}
public static bool IsFizz(int number)
{
double value = number / 3.0;
return CheckForZeroDecimal(value);
}
public static bool IsBuzz(int number)
{
double value = number / 5.0;
return CheckForZeroDecimal(value);
}
public static bool CheckForZeroDecimal(double value)
{
var formattedNumber = string.Format("{0:N1}", value);
int dotIndex = formattedNumber.IndexOf(",", StringComparison.Ordinal);
return formattedNumber.Substring(dotIndex + 1, 1) == "0";
}
public static string FizzBuzz(int number)
{
string s = null;
if (IsFizz(number))
s += "fizz";
if (IsBuzz(number))
s += "buzz";
return s ?? number.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment