jae-l
6/26/2017 - 1:49 PM

Example of how to calculate the factorial of an integer using recursion.

Example of how to calculate the factorial of an integer using recursion.

static void Main()
{
  Console.Write("Enter the integer to calculate the factorial for: ");
  int num = int.Parse(Console.ReadLine());
  
  int factorial = Factorial(num);
  
  Console.WriteLine("{0}! = {1}", num, factorial);
}

static int Factorial(int num)
{
  if (num == 0)
  {
    return 1;
  }
  else
  {
    return num * Factorial(num - 1);
  }
}

//  Enter the integer to calculate the factorial for: 4
//  4! = 24