Last active
October 12, 2022 20:14
-
-
Save tugberkugurlu/4390966 to your computer and use it in GitHub Desktop.
FizzBuzz problem solution with C#.
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
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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}")}");
}