## Featured Replies in this Discussion

Jugortha, before you post any code should you not test it yourself?**First: **In C#, when you declare a variable in a for loop (in this case **i**) it is only known in the for loop not outside it(strict scoping rules in C#, I like that)

The return statement has not the faintest idea what **i** is.**Second:** What's the use of incrementing a variable **i** in a for loop and multiplying it by one? It certainly will not give you a factorial!

hello,

o The user enters a number and clicks the Calculate button or presses the Enter key to activate that button.

o The program calculates the factorial of the number and displays it in a label on the form.

o The factorial of an integer is that integer multiplied by every positive integer less than itself. A factorial number is identified by an exclamation point following the number. Here's how you calculate the factorial of the numbers 1 through 5:

1! = 1 which equals 1

2! = 2 * 1 which equals 2

3! = 3 * 2 * 1 which equals 6

4! = 4 * 3 * 2 * 1 which equals 24

5! = 5 * 4 * 3 * 2 * 1 which equals 120

o The application should return an accurate value for integers from 1 to 20. The factorial of the number 20 is 2,432,902,008,176,640,000. (If you enter a number larger than 20, a factorial will still be displayed, but it won't be accurate.)

o The application assumes that the user enters a valid integer from 1 to 20.

o The application should format the factorial with commas, but no decimal places

please reply me i have submission tomorrow..

Holy resurrection, this thread is almost 5 years old.

That said, how to calculate a factorial has already been given. Since you are doing small number factorials (20 is small) the methods here will be fast enough. All you need to do is figure out what numeric type to use that will hold a value of 2.5 quintillion.

TRY

THIS CODE IN CONSOLE APLICATION

```
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static int factorial(int number)
{
if (number <= 1)
return 1;
else return number * factorial(number - 1);
}
static void Main(string[] args)
{
Console.WriteLine("The Factorial of 5 is {0}\n", factorial(5));
Console.WriteLine("The Factorial of 10 is {0} \n",factorial(10));
Console.ReadLine();
}
}
}
```

Hello,

I am happy to see this, I am a New student of C#, This Problem is what I used as my Assignment on Friday, Below is the Simple Code, that I Used for it, Hope it work for you as well,.... thank

```
private void btnCalculate_Click(object sender, EventArgs e)
{
bool flag;
double start;
double formula;
double answer;
double j;
flag = double.TryParse(txtNumberTo.Text, out start); // This is to make sure that User Input is Digit Data.
if(flag==false)
{
MessageBox.Show("Numeric Data Only", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtNumberTo.Focus();
txtNumberTo.Clear();
return;
}
answer = start;
for (j = 0; j < start; j++) // This is the Code that Do the Factorial Work.
{
formula = start - j;
answer *= formula;
}
lblAnswer.Text = start.ToString() + "!" + " = " + answer.ToString(); // This will be your Output, likr N! = XXXX eg 5! = 120
}
```

Could somebody help for the problem is I code for invalid number input message but it won't show up message "invalid numbe"?

Here my code:

```
private void Calculate_Click(object sender, EventArgs e)
{
long number = Convert.ToInt64(txtNumber.Text); // get number input from text box number
long factorial = 1;
// calculate factorial
while ( number > 0 && number <= 20 )
{
factorial *= number;
number--;
} // end while loop
if (number < 0 && number > 20)
{
lblFactorial.Text = "invalid number";
}
{
lblFactorial.Text = factorial.ToString("n0");
}
}
private void Exit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtNumber.Clear();
}
}
}
```