编写程序计算S=1+1/1!+1/2!+1/3!......+1/n!
时间: 2023-12-07 09:38:43 浏览: 220
以下是Python编写程序计算S=1+1/1!+1/2!+1/3!......+1/n!的方法:
```python
import math
n = int(input("请输入正整数n:"))
s = 0
for i in range(n+1):
s += 1/math.factorial(i)
print("S的值为:", s)
```
相关问题
C语音编写程序计算s=1+1/2!+1/3!+...+1/n!
以下是使用C语言编写程序计算s=1+1/2!+1/3!+...+1/n!的例子:
```c
#include <stdio.h>
// 计算阶乘
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n;
double sum = 1.0;
printf("请输入一个整数n:");
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
sum += 1.0 / factorial(i);
}
printf("计算结果为:%lf\n", sum);
return 0;
}
```
编写c语言程序计算S=1+1/1!+1/2!+1/3!......+1/n!
以下是一个C语言程序,用于计算S=1+1/1!+1/2!+1/3!......+1/n!:
```c
#include <stdio.h>
int main() {
int n, i, j;
double sum = 1, fac = 1;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fac *= i;
sum += 1.0 / fac;
}
printf("S的值为:%lf", sum);
return 0;
}
```
阅读全文