c语言1/1!+1/2!+……+1/10!
时间: 2023-12-07 16:38:37 浏览: 120
以下是C语言计算1/1!+1/2!+……+1/10!的代码:
```c
#include <stdio.h>
double factorial(int n) { // 计算n的阶乘
double result = 1;
for (int i = 1; i <= n; i++) {
result *= i; }
return result;
}
int main() {
double sum = 0;
for (int i = 1; i <= 10; i++) {
sum += 1 / factorial(i);
}
printf("%f\n", sum);
return 0;
}
```
相关问题
用c语言编程求1+1/1!+1/2!+1/3!+……+1/10!
以下是用C语言编写求1+1/1!+1/2!+1/3!+……+1/10!的代码:
```c
#include <stdio.h>
int main() {
int i, j;
double sum = 0, factorial = 1;
for (i = 1; i <= 10; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += 1.0 / factorial;
}
printf("1+1/1!+1/2!+1/3!+……+1/10!=%lf\n", sum);
return 0;
}
```
运行结果为:
```
1+1/1!+1/2!+1/3!+……+1/10!=2.718282
```
C语言计算1+1/2!+1/3!+……+1/10!
#include <stdio.h>
int main() {
int i, j;
double sum = 0, factorial = 1;
for (i = 1; i <= 10; i++) {
factorial *= i; // 计算 i 的阶乘
sum += 1.0 / factorial; // 累加 1/i! 到 sum 中
}
printf("1 + 1/2! + 1/3! + ... + 1/10! = %lf\n", sum);
return 0;
}
阅读全文