用c语言计算(2)1!+2!+3!+4!+……+n! n=20
时间: 2023-11-24 22:51:54 浏览: 76
以下是C语言计算(2)1!+2!+3!+4!+……+n! n=20的代码:
```c
#include <stdio.h>
int main() {
int i, j;
long long sum = 0, fac = 1;
for (i = 1; i <= 20; i++) {
fac = 1;
for (j = 1; j <= i; j++) {
fac *= j;
}
sum += fac;
}
printf("(2)1!+2!+3!+4!+……+n! n=20: %lld", sum * 2);
return 0;
}
```
相关问题
用C语言计算:1!+2!+3+……+n!(用双层循环完成)
以下是用C语言计算1!+2!+3+……+n!(用双层循环完成)的代码实现:
```c
#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;
}
int main() {
int n;
printf("您需要1到多少的阶乘之和,请输入(此值请小于26)\n");
scanf("%d", &n);
printf("数字1到数字%d各阶乘之和为%d\n", n, fun(n));
system("pause");
return 0;
}
```
c语言编程计算1!+2!+3!+……+6!
以下是两种C语言编程计算1!+2!+3!+……+6!的例子:
引用:
```c
#include <stdio.h>
int main() {
int i = 0;
int result = 1;
int sum = 0;
for (i = 1; i < 7; i++) {
result *= i;
sum += result;
}
printf("1!+2!+3!+……+6!:%d", sum);
return 0;
}
```
引用:
```c
#include <stdio.h>
int main() {
int i = 0;
int j = 0;
int n = 0;
int sum = 0;
int result = 1; for ( i = 1; i < 7; i++) {
result = 1;
for (j = 1; j <= i; j++) {
result *= j;
}
sum += result;
}
printf("1!+2!+3!+……+6!:%d", sum);
return 0;
}
```
阅读全文