Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Factorial Simplified using lambda

4.20/5 (7 votes)
2 May 2011CPOL 18.7K  
Factorial Simplified using lambda
Here is a trick which uses recursion in Lambda:

C#
class Factorial
   {
       static void Main()
       {
           //Lambada Expression
           Func<int,int> call = null;
           call = x => x * (x == 1 ? 1 : call(x - 1));
           Console.WriteLine(call(5));
           Console.ReadLine();
       }
   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)