Last active
August 29, 2015 14:13
-
-
Save maads/10e804bcd2e2341b8e64 to your computer and use it in GitHub Desktop.
FizzBuzz division
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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