12,944,475 members (52,850 online)
alternative version

#### Stats

77.6K views
4 bookmarked
Posted 3 Oct 2011

# Calculate the Factorial of an Integer in C#

, 4 Oct 2011 CPOL
 Rate this:
I looked for a while over the net for a way to easily calculate a factorial value (n!), but nothing was helping and I saw some VERY long and drawn out solutions. Well, it wasn't that hard after I thought about it for a while and this was the easiest solution I could come up with. I hope it helps!

The code below is what I put in my event handler. The comment lists my class variables used.

```int Factorial(int input)
{
int answer = 0;

if (input > 0)
{
count = 1;
while (count <= input)
{
if (count == 1)
{
count++;
}
else
{
count++;
}
}
}
else
{
MessageBox.Show("Please enter only a positive integer.");
}

}```

## Comments and Discussions

 First Prev Next
 Linq Version raulito17-Oct-16 5:30 raulito 17-Oct-16 5:30
 Check this one Member 1096039624-Dec-14 4:26 Member 10960396 24-Dec-14 4:26
 Fail for higher order numbers CyberSamuraiii13-May-14 7:21 CyberSamuraiii 13-May-14 7:21
 Zero factorial is not undefined Member 806338616-Aug-13 3:06 Member 8063386 16-Aug-13 3:06
 Slight Improvement markbb3322-Feb-13 18:36 markbb33 22-Feb-13 18:36
 My vote of 1 fujiwara129-Oct-12 3:26 fujiwara1 29-Oct-12 3:26
 Reason for my vote of 1 Really you couldn't find any iterati... Kerem Kat12-Oct-11 8:55 Kerem Kat 12-Oct-11 8:55
 I've reworked the method to make it more generic and cleaner... Chris Maunder4-Oct-11 2:47 Chris Maunder 4-Oct-11 2:47
 Shouldn't the tbxAnswer.Text = total.ToString("n0");
 Last Visit: 31-Dec-99 18:00     Last Update: 23-May-17 2:59 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.