c语言求7!+8!+9!
时间: 2023-11-17 10:02:22 浏览: 100
C语言求7!+8!+9!的方法如下:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int sum = 0;
int i;
for (i = 7; i <= 9; i++) {
int j;
int mul = 1;
for (j = 1; j <= i; j++) {
mul *= j;
}
sum += mul;
}
printf("%d", sum);
system("pause");
return 0;
}
```
首先,我们使用一个for循环来遍历7到9,然后在内部使用另一个for循环来计算每个数的阶乘,并将它们相加。最后,我们打印出结果。
相关问题
c语言求1!+2!+3! +4! +5! +6! +7! +8! +9! +10! 的值
以下是C语言求1!+2!+3! +4! +5! +6! +7! +8! +9! +10! 的值的代码:
```c
#include <stdio.h>
int main() {
int i, j, sum = 0, factorial = 1;
for (i = 1; i <= 10; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
printf("1!+2!+3!+...+10! = %d", sum); // 输出:1!+2!+3!+...+10! = 4037913
return 0;
}
```
用C语言求1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值
以下是使用C语言求1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值的代码示例:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += factorial(i);
}
printf("The sum of factorials from 1 to 10 is: %d\n", sum);
return 0;
}
```
运行结果:
```
The sum of factorials from 1 to 10 is: 4037913
```
阅读全文