Programmermid - 1 year ago 151
C# Question

# calculate factorial in C# using recursion

I know how to calculate factorial using a loop. Below is the code for loop but I am getting error while doing it by recursion, below are both the codes can somebody sugg

`````` namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);

}
}
}
``````

Factorial using Recursion :

est something as where I am going wrong? When I am calculating it using recursion?

Factorial using loop:

``````public double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number * factorial_recursion(number - 1);
}

public double factorial_WhileLoop(int number)
{
double result = 1;
while (number != 1)
{
result = result * number;

}
return result;
}
``````

``````factorial_Recursion is the method name.