15,944,835 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

# Calculate the Factorial of an Integer in C#

Rate me:
4 Oct 2011CPOL 54.7K   2   12
It could be done using Recursive Lambda Expression, for example,class Program{ static Func Factorial = x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1); static void Main(string[] args) { Console.WriteLine(Factorial(5)); }}(The code...
It could be done using Recursive Lambda Expression, for example,

C#
```class Program
{
static Func<int, int> Factorial = x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1);

static void Main(string[] args)
{
Console.WriteLine(Factorial(5));
}
}```

(The code is based on the References sites.)

From the above code, `Factorial `is variable of type `Func<int,int>` which is holding a anonymous method. So following line of code...
```x => x < 0 ? -1 : x == 1 || x == 0 ? 1 : x * Factorial(x - 1)
```

...is the anonymous method and is equivalent to:
C#
```int Factorial( int x ){
if( x<0){
return -1;
}else if( x==1 || x==0 )
{
return 1;
}else{
return x* Factorial(x-1);
}
}```

It is also possible to generate a Factorial for a number without recursion, for example, the following code block will calculate without the recursive method call:

C#
```private static int Factorial(int i)
{
int result = i < 0 ? -1 : i == 0 || i == 1 ? 1 : 1;
if (i > 0)
Enumerable.Range(1, i).ToList<int>().ForEach(element => result = result * element);
return result;
}```

and also using `Aggregate` function of Linq:

C#
```private static int Factorial(int i)
{
return i < 0 ? -1 : i == 0 || i == 1 ? 1 : Enumerable.Range(1, i).Aggregate((counter, value) => counter * value);
}```

(initial idea to use `Aggregate `function found in here[^].)

References

[1] http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx[^]
[2] http://en.wikipedia.org/wiki/Recursion[^]
[3] Factorial Simplified using lambda[^]
[4] http://stackoverflow.com/questions/1426715/factorial-of-n-numbers-using-c-lambda[^]
:)

Written By
Software Developer
Australia
Designer and Architect.
Author of the

## Comments and Discussions

 First Prev Next
 recursive lambda Member 443784318-May-20 3:36 Member 4437843 18-May-20 3:36
 recursive lambda Member 443784318-May-20 1:43 Member 4437843 18-May-20 1:43
 Reason for my vote of 1 advocating recursion and barely read... dmjm-h26-Oct-11 6:30 dmjm-h 26-Oct-11 6:30
 I gave it 5, my reason: mind = blown. The first algorithm wa... KenBonny17-Oct-11 21:35 KenBonny 17-Oct-11 21:35
 Re: Thank you very much. :) Mohammad A Rahman17-Oct-11 22:26 Mohammad A Rahman 17-Oct-11 22:26
 Reason for my vote of 5 Reason for My vote of 5 I liked it.. RaviRanjanKr12-Oct-11 11:03 RaviRanjanKr 12-Oct-11 11:03
 Re: Thank you very much Ravi :) Mohammad A Rahman12-Oct-11 11:54 Mohammad A Rahman 12-Oct-11 11:54
 Nooooooooooo! Please, no more recursive functions for factor... Chris Maunder4-Oct-11 2:43 Chris Maunder 4-Oct-11 2:43
 nice kiran dangar4-Oct-11 0:31 kiran dangar 4-Oct-11 0:31
 Re: Thanks. Mohammad A Rahman4-Oct-11 0:37 Mohammad A Rahman 4-Oct-11 0:37
 Good solution :) I would be interested to see someone do spe... Ed Nutting3-Oct-11 20:28 Ed Nutting 3-Oct-11 20:28
 Re: Thanks Edward :) Mohammad A Rahman3-Oct-11 22:09 Mohammad A Rahman 3-Oct-11 22:09
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Jul-24 20:33 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.