帮我看看这个是得到1×2×3×…×10吗?

来源:百度知道 编辑:UC知道 时间:2024/07/04 20:28:00
int i=1; s=1;
do{s=s*i;
i++;
}while(i<=10);
不是1×2×3×…×10×11吗?

程序是正确的。
因为在do循环里做了s=s*i后i++;了。
如果i=9,i++后就是10,小于等于10,还会做s=s*10,然后====》
此时i=10,i++后就是11了,大于10不会做下次do循环。

对,

是的。

[38@c-cpp]$ cat test.c
#include <stdio.h>
int main()
{
int i=1;
int s=1;
do{
printf("%d ", i);
s=s*i;
i++;
}while(i<=10);
printf("%d\n", s);
}
[39@c-cpp]$ gcc test.c
[40@c-cpp]$ ./a.out
1 2 3 4 5 6 7 8 9 10 3628800

是,不过你的定义得改改int i=1, s=1;

第一句定义错了:int i=1; s=1; 分号改为逗号
其他的是对的

#include "stdio.h"
main()
{
int i=1,s=1;
do{s=s*i;
i++;
}
while(i<=10);
printf("%d\n",s);
}