turbo_c_2 于 2014.12.10 15:59 提问

c语言递归求阶乘 ，不知道哪里不对

#include
long f(i)
{ long f=1;
while(i>=1)
f*=f(i-1);
return f;
}
main()
{ int i=10;
printf("factorial=%d",f(i));
}

10个回答

xsm_lyf   2014.12.10 16:32

= =你的i传进去 就是为了判断的吗？
while(i>1)
f=i* f(i-1)

lxq_xsyu   2014.12.10 16:19

#include

long f(int i){
if(i == 1)
return 1;
else
i*= f(i-1);
return i;
}

int main(){
int i = 10;
printf("factorial=%d", f(i));

``````scanf("%d");
``````

}

``````
``````
lxq_xsyu   2014.12.10 16:20

w810443616   2014.12.10 16:35

#include
long fun(int i)
{
long f = i;
if(i == 1)
return 1;
f *= fun(i-1);
return f;
}
main()
{
int i = 10;
printf("factorial=%d",f(i));
}

windsunmoon   2014.12.10 17:30
`````` #include
long f(i)
{
if(i==1)
return 1;

return i*f(i-1);
}
main()
{ int i=10;
printf("factorial=%d",f(i));
}

``````
qq_22002947   2014.12.10 18:22
``````这样就好了吧。。。
i*f（i-1)的话。。。求的是什么啊。。。
``````
``````    if(i == 0) return 1;
else return i*f(i-1);
``````
u012421456   2014.12.10 20:00
`````` # include <stdio.h>

int f(int i)
{
int t = i;
if (1 == i)
{
return 1;
}

t *= f(i-1);

return t;
}

int main(void)
{
int i=10;
printf("factorial=%d\n",f(i));

return 0;
}
``````
xiaohuoxiaoer   2014.12.11 10:39

#include
long f(int i)
{ long a=1;
a*=f(i-1);
return a;
}
main()
{ int i=10;
printf("factorial=%d",f(i));
}

zxh707wk   2014.12.11 19:57

#include

int funiction(int num)
{
if(!num)return 1;
return num*funiction(num-1);
}

int main()
{
printf("factorial=%d",funiction(10));
return 0;
}

``````
``````
Oraice   2014.12.11 22:21

/*错误有一下几点：1.函数包含的输入输出头文件没有。2.实参的内容传递给形参时，实参的变量不能与形参的重名，毕竟形参是复制的实参里的值。*/
#include"stdio.h"

long factorial(long number);
int main(){
int i;

``````for(i = 0; i < 10; i++){
printf("%ld\n", factorial(i));
}

return 0;
``````

}
long factorial(long number){
if(number <= 1){
return 1;
}else{
return (number * factorial(number -1 ));
}
}