csharp 如何使用递归计算整数的阶乘的示例。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 如何使用递归计算整数的阶乘的示例。相关的知识,希望对你有一定的参考价值。

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

以上是关于csharp 如何使用递归计算整数的阶乘的示例。的主要内容,如果未能解决你的问题,请参考以下文章