为什么这个阶乘计划有效?

时间:2015-08-25 23:13:16

标签: c# recursion factorial

这是一个在C#教程教程中显示的阶乘程序,我理解递归的基本概念,但我不确定为什么它在这种情况下有效。该计划是这样的:

public int factorial(int num)
{
    /* local variable declaration */
    int result;
    if (num == 1)
    {
        return 1;
    }
    else
    {
        result = factorial(num - 1) * num;
        return result;
    }
}

static void Main(string[] args)
{
    NumberManipulator n = new NumberManipulator();
    //calling the factorial method
    Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6));
    Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
    Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
    Console.ReadLine();
}

0 个答案:

没有答案