用递归方法求n的阶乘

Posted urahyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用递归方法求n的阶乘相关的知识,希望对你有一定的参考价值。

代码:

#include<iostream>
using namespace std;
int fact(int n);
int main()
{
    int n;
    loop:
    cin >> n;
    cout << fact(n);
    goto loop;
}
int fact(int n)
{
    if (n == 0)       //递归终止条件
    {
        return 1;
    }
    return n * fact(n - 1);
}

 

以上是关于用递归方法求n的阶乘的主要内容,如果未能解决你的问题,请参考以下文章