Last active
June 18, 2020 06:53
-
-
Save szewa-polyu/3d0af0bb07e7bc3f52a5ec4cc225f870 to your computer and use it in GitHub Desktop.
FizzBuzz - C# Pattern Matching Public
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
using System; | |
using System.Linq; | |
public class C | |
{ | |
public static void Main() | |
{ | |
foreach (var i in Enumerable.Range(1, 100)) | |
{ | |
Console.WriteLine(GetFizzBuzz(i)); | |
} | |
} | |
private static bool IsDivisible(int divident, int divisor) | |
{ | |
return divident % divisor == 0; | |
} | |
private static bool IsDivisibleByThree(int i) | |
{ | |
return IsDivisible(i, 3); | |
} | |
private static bool IsDivisibleByFive(int i) | |
{ | |
return IsDivisible(i, 5); | |
} | |
private static string GetFizzBuzz(int i) | |
{ | |
return (IsDivisibleByThree(i), IsDivisibleByFive(i)) switch | |
{ | |
(true, false) => "Fizz", | |
(false, true) => "Buzz", | |
(true, true) => "FizzBuzz", | |
_ => i.ToString() | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment