1!+2!+....+n!C语言
时间: 2023-11-24 21:51:50 浏览: 65
以下是求1!+2!+....+n!的C语言代码:
```c
#include <stdio.h>
int main() {
int n, i;
double sum = 0, factorial = 1;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
sum += factorial;
}
printf("1!+2!+....+n!的值为:%lf", sum);
return 0;
}
```
相关问题
计算1!+2!+3!+......+n!用c语言
以下是计算1!+2!+3!+......+n!的C语言代码:
```c
#include <stdio.h>
int main()
{
int n, i, j;
long long fact = 1, sum = 0;
printf("请输入一个正整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact = 1;
for (j = 1; j <= i; j++) {
fact *= j;
}
sum += fact;
}
printf("1!+2!+3!+...+%d! = %lld\n", n, sum);
return 0;
}
```
利用公式+e=1+11!+12!+13!+...+1n!e=1+11!+12!+13!+...+1n!,+求+ee。c语言
根据提供的引用,可以使用C语言编写程序求无理数e的值并输出,计算公式为:e=1+1/1!+1/2!+1/3!+...+1/n!,当1/n! < 0.000001时e=2.718282。
以下是C语言程序的示例代码:
```c
#include <stdio.h>
double factorial(int n) { // 递归实现阶乘
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
double e = 1;
double item = 1;
int n = 1;
while (item >= 0.000001) {
item = 1.0 / factorial(n);
e += item;
n++;
}
printf("e = %lf\n", e);
return 0;
}
```
运行结果为:
```
e = 2.718282
```
阅读全文