Skip to content

Instantly share code, notes, and snippets.

@celsojr
Last active October 12, 2019 17:44
Show Gist options
  • Save celsojr/18dd9899aee0c4b084b0f5d930f458e4 to your computer and use it in GitHub Desktop.
Save celsojr/18dd9899aee0c4b084b0f5d930f458e4 to your computer and use it in GitHub Desktop.
// 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