gpt4 book ai didi

c - 阶乘与递归

转载 作者:行者123 更新时间:2023-11-30 18:10:50 25 4
gpt4 key购买 nike

#include <stdio.h>
long int fact(int n);

int main()
{
int n;
printf("Enter number\n");
scanf("%d",&n);
printf("Factorial:%ld\n",fact(n));
return 0;
}


long int fact(int n)
{
if(n!=1)
return n*fact(n-1);

}

我试图用递归得到一个数字的阶乘。但我每次都得到0作为结果。这段代码有什么问题?

最佳答案

long int fact(int n)
{
return n==1? 1 : n*fact(n-1);
}

关于c - 阶乘与递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54963355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com