编写程序,计算Sn=1+1/2+1/3+…
时间: 2023-10-06 07:07:59 浏览: 191
编写程序sy4-1.c,输入n,计算并输出如下多项式的值:Sn=1 1/2! 1/3! 1/4! … 1/n!。以下是一个示例程序,你可以根据自己的需求进行修改和调试:
#include <stdio.h>
int main() {
int n;
double Sn = 0.0, factorial = 1.0;
printf("请输入n的值:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
factorial *= i;
Sn += 1 / factorial;
}
printf("Sn的值为:%f\n", Sn);
return 0;
}
相关问题
编写程序计算sn=1+1/2+1/3+1/4+1/5
Sure, the program to calculate sn=1 1/2 1/3 1/4 1/5 is quite simple. Here's a Python code snippet that computes sn.
```
n = 5
sum_numbers = 0.0
for i in range(1, n+1):
sum_numbers += 1/i
print(sum_numbers)
```
The output will be 2.283333333333333.
编写c语言程序计算sn=1+1/2+1/3+1/4+1/5
好的,这个问题非常简单。以下是C语言程序:
#include <stdio.h>
int main() {
int n=5;
float sum=0;
for(int i=1;i<=n;i++){
sum+=1.0/i;
}
printf("The sum of the series is %f",sum);
return 0;
}
程序输出:The sum of the series is 2.283333。希望对您有帮助!
阅读全文