c语言求s=1!+ 2!+ 3!+ ...+10!,并输出(请分别用双层循环和单层循环解决)
时间: 2024-06-16 14:06:14 浏览: 124
c++ 1!+2!+3!+..
C语言求s=1!+ 2!+ 3!+ ...+10!的问题可以使用双层循环和单层循环两种方式解决。
1. 双层循环解决:
```c
#include <stdio.h>
int main() {
int i, j;
int factorial, sum = 0;
for (i = 1; i <= 10; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
printf("双层循环解决,s的值为:%d\n", sum);
return 0;
}
```
2. 单层循环解决:
```c
#include <stdio.h>
int main() {
int i;
int factorial = 1, sum = 0;
for (i = 1; i <= 10; i++) {
factorial *= i;
sum += factorial;
}
printf("单层循环解决,s的值为:%d\n", sum);
return 0;
}
```
阅读全文