Programmermid Programmermid - 29 days ago 5
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");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();

}
}
}


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;
}

Answer

your call name is not equal to you method name.

factorial_Recursion is the method name.
factorial_recursion is the call