Last active
October 12, 2019 17:44
-
-
Save celsojr/18dd9899aee0c4b084b0f5d930f458e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// C# function to calculate the factorial of a given number. | |
using System.Linq; | |
using System.Collections.Generic; | |
using static System.Console; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var numbers = 0.To(10); | |
numbers.ForEach(x => WriteLine($"{x:00}! = {x.Fact()}")); | |
} | |
} | |
public static class ExtensionsMethods | |
{ | |
public static long Fact(this long x) => x <= 1 ? 1 : x * Fact(x - 1); | |
public static IEnumerable<long> To(this int st, int ct) | |
{ | |
for (var x = st; x <= ct; x++) | |
{ | |
yield return x; | |
} | |
} | |
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) | |
{ | |
foreach (T item in source) | |
action(item); | |
} | |
} | |
/* the output | |
00! = 1 | |
01! = 1 | |
02! = 2 | |
03! = 6 | |
04! = 24 | |
05! = 120 | |
06! = 720 | |
07! = 5040 | |
08! = 40320 | |
09! = 362880 | |
10! = 3628800 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment