编写程序计算sn=1+1/2+1/3+1/4+1/5
时间: 2023-09-12 20:03:10 浏览: 218
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。希望对您有帮助!
编写程序计算sn=1+1/2+1/3....+1/n
以下是C语言实现计算Sn的程序:
```c
#include <stdio.h>
double fun(int n) {
double sn = 0.0;
int i;
for (i = 1; i <= n; i++) {
sn += 1.0 / i;
}
return sn;
}
int main() {
int n = 15;
double sum = fun(n);
printf("sum is %f\n", sum);
return 0;
}
```
运行结果:
```
sum is 3.318228
```
阅读全文