把while(n==1) 改为 while(n>=1),否则的话,如果你输入的是5,那第一次执行循环的时候条件就不成立,根本就没进行阶乘的运算。
还有,那个while(0) { 这两个去了吧,画蛇添足,还添错了,while(0)这条件永远都不会成立。改完后的函数体:
int n;
int count=0; //其实count变量定义的没用,可以去掉
long op=1;
printf("Please input the n for jiecheng:\n");
scanf("%d",&n);
while(n>= 1)
{
op=op * n;
n=n - 1;
count++; //如果上面的count定义去掉,那么这条也可以去掉了
}
printf("THE n!is %ld",op);
return 0; //你这个地方原来缺少个分号
你需要改几个地方:
第一个while(0) 改为 while(1)
while(n == 1) 改为while(n > 1)
将long op = 1 写到while(1)里面
如下:
void main()
{
int n;
int count=0;
long op=1;
while(1)
{
op = 1;
printf("Please input the n for jiecheng:\n");
scanf("%d",&n);
while(n != 1)
{
op=op * n;
n=n - 1;
count++;
}
printf("THE n!is %ld\n",op);
}
}
#include
int main(){
int n;
int count=0;
long op=1;
printf("Please input the n for jiecheng:\n");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
op = op*i;
}
printf("THE n!is %ld",op);
return 0;
}
……
while(1) //你的第一个while括号里的是0,不执行,改为1,或者其他的数都行,
就可以 循环求阶乘!
{
printf("Please input the n for jiecheng:\n");
scanf("%d",&n);
while(n!=1) //当条件不满足不等于1时跳出,就是n等于1时
{
op=op * n;
n - -;
}
printf("THE n! is %ld",op); //大括号在这里,表示第一次的循环结束,然后主函数返回
}
return 0;
}
main()
{
float n,s=0,t=1;
for(n=1;n<=20;n++)
{
t*=n;
s+=t;
}
printf("1+2!+3!...+20!=%e\n",s);
}
刚做的
你这个程序的问题首先就体现在了while(0)......如果用while(0)你永远都不可能执行while循环体内的语句,你再好好思考下吧