版权声明:本文为博主原创文章,转载请附上本博文链接。 https://blog.csdn.net/weixin_41704182/article/details/88251388
阶乘函数
- 阶乘函数可用递归定义为:
python代码
def Factorial(n):#计算n的阶乘
if int(n)<= 1:
return 1
else:
return n*Factorial(n-1)
if __name__ == "__main__":
n=int(input('Please input a number\n'))
print(Factorial(n))
C++代码
#include<iostream>
using namespace std;
int Factorial(int n);
int Factorial(int n)
{
if(n<=1)
{
return 1;
}
else
{
return n*Factorial(n-1);
}
}
int main()
{
int n;
cout<<"请输入n:"<<endl;
cin>>n;
cout<<"n的阶乘n!= "<<Factorial(n)<<endl;
return 0;
}
```