编程计算S=1!+2!+3!+4!+…+10!的值。c语言
时间: 2023-11-17 10:01:59 浏览: 70
以下是C语言编写计算S=1!+2!+3!+4!+…+10!的程序:
```c
#include <stdio.h>
int main() {
int sum = 0, n = 10, a = 1, i, j;
for (i = 1; i <= n; i++) {
a = 1;
for (j = 1; j <= i; j++) {
a = a * j;
}
sum = sum + a;
}
printf("S的值为:%d\n", sum);
return 0;
}
```
相关问题
编程计算S=1!+2!+3!+4!+…+10!的值。c语言编译
以下是两种C语言编译计算S=1!+2!+3!+4!+…+10!的值的例子:
引用:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int fun(int x) {
int Sn = 0;
int S;
for (int i = 1; i <= x; ++i) {
S = 1;
for (int j = 1; j <= i; ++j) {
S *= j;
}
Sn += S;
}
return Sn;
}
void main() {
int n;
printf("您需要1到多少的阶乘之和,请输入(此值请小于26)\n");
scanf("%d", &n);
printf("数字1到数字%d各阶乘之和为%d\n", n, fun(n));
system("pause");
}
```
引用:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int fun(int x) {
int Sn = 0;
int S = 1;
for (int i = 1; i <= x; ++i) {
S = i*S;
Sn += S;
}
return Sn;
}
void main() {
int n;
printf("您需要1到多少的阶乘之和,请输入(此值请小于26)\n");
scanf("%d", &n);
printf("数字1到数字%d各阶乘之和为%d\n", n, fun(n));
system("pause");
}
```
编程计算S=1!+2!+3!+4!+…+10!的值。用C语言
以下是C语言计算S=1!+2!+3!+4!+…+10!的代码:
```c
#include <stdio.h>
int main() {
int i, j;
long long factorial, sum = 0;
for (i = 1; i <= 10; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
printf("S = %lld\n", sum);
return 0;
}
```