Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Last active October 12, 2022 20:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tugberkugurlu/4390966 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/4390966 to your computer and use it in GitHub Desktop.
FizzBuzz problem solution with C#.
// reference: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
static class Program {
static void Main(string[] args) {
for (int i = 1; i <= 100; i++) {
bool canBeMultipliedByFive = i.CanBeMultipliedBy(5);
bool canBeMultipliedByThree = i.CanBeMultipliedBy(3);
if(canBeMultipliedByFive && canBeMultipliedByThree) {
Console.WriteLine("{0}: FizzBuzz", i);
}
else if(canBeMultipliedByThree) {
Console.WriteLine("{0}: Fizz", i);
} else if(canBeMultipliedByFive) {
Console.WriteLine("{0}: Buzz", i);
} else {
Console.WriteLine(i);
}
}
Console.ReadLine();
}
private static bool CanBeMultipliedBy(this int sourceNumber, int targetNumber) {
return (sourceNumber % targetNumber) == 0;
}
}
@winslowj
Copy link

for (var i = 1; i <= 100; i++)
{
Console.WriteLine(i%3==0&&i%5==0?"Fizz Buzz":$"{(i%3==0?"Fizz":i%5==0?"Buzz":$"{i}")}");
}

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