c语言s=1*2*3*....*100
时间: 2024-06-18 11:06:09 浏览: 10
这是一个计算1到100的阶乘的程序,即1*2*3*...*100的结果。C语言代码如下:
```c
#include <stdio.h>
int main() {
int i, fact = 1;
for (i = 1; i <= 100; i++) {
fact *= i;
}
printf("1到100的阶乘为:%d\n", fact);
return 0;
}
```
在上面的代码中,使用了for循环从1到100遍历每一个数,并计算它们的乘积。在每次循环中,将当前数乘以已经计算出来的乘积,并将结果赋值给乘积变量fact。最终,fact就是1到100的阶乘。
相关问题
C语言s=1*2*3*4*……*100
你好!要计算1*2*3*4*……*100的结果,可以使用循环结构来实现。以下是C语言的代码示例:
```c
#include <stdio.h>
int main() {
int i;
long long factorial = 1;
for (i = 1; i <= 100; ++i) {
factorial *= i;
}
printf("1*2*3*4*...*100 = %lld\n", factorial);
return 0;
}
```
这段代码使用了一个循环来计算阶乘的结果,初始值为1,然后依次乘以2、3、4、...、100。最后,将结果打印出来。
用c语言设s=1*2*3*4*.....*n,编程求s不大于40000时最大的n。
可以使用循环来逐个计算 s,直到 s 不大于 40000 为止。具体实现如下:
```c
#include <stdio.h>
int main() {
int n = 1;
long long s = 1;
while (s <= 40000) {
s *= n;
n++;
}
printf("n = %d\n", n - 1);
return 0;
}
```
在上面的代码中,我们首先初始化 n 和 s 为 1,然后在 while 循环中,每次将 s 乘以 n,同时将 n 加 1,直到 s 不大于 40000 为止。最后输出 n 的值,即为所求的最大的 n。注意,这里用了 long long 类型来存储 s,是为了防止 s 溢出。