用递归编程方法求n的阶乘n!

2024-11-07 04:46:43
推荐回答(2个)
回答1:

#include
int jiecheng(int b)
{
if(b==1||b==0)
return 1;
else
return b*jiecheng(b-1);
}
void main()
{
printf("请输入要求哪个数的阶乘:\n");
int n;
int result;
scanf("%d",&n);
result=jiecheng(n);
printf("%d的阶乘为%d \n",n,result);
}

回答2:

python:

def fac(n):
    if n==0 or n==1:
        return 1
    else:
        return n*fac(n-1)