c语言数列求和2/1+3/2+5/3
时间: 2023-11-23 15:07:38 浏览: 153
以下是C语言计算数列2/1+3/2+5/3的和的代码:
```c
#include <stdio.h>
void main(){
double x=2,y=1,sum=0,t;
int i,n;
printf("请输入求和通项:");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum+=x/y;
t=x;
x=y+x;
y=t;
}
printf("2/1,3/2,5/3,8/5,13/8,21/13……前%d项和为:%f \n",n,sum);
}
```
运行结果:
```
请输入求和通项:3
2/1,3/2,5/3,8/5,13/8,21/13……前3项和为:4.166667
```
相关问题
c语言数列求和1-1/2+1/3
以下是C语言求解数列1-1/2+1/3的和的代码和运行结果:
```c
#include <stdio.h>
int main() {
int n, i;
double sum = 0.0, sign = 1.0;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += sign * 1.0 / i;
sign = -sign;
}
printf("1-1/2+1/3-1/4+1/5-1/6+1/7-...+1/n的和为:%.4lf\n", sum);
return 0;
}
```
运行结果:
```
请输入n的值:5
1-1/2+1/3-1/4+1/5-1/6+1/7-...+1/n的和为:0.7833
```
c语言数列求和1-1/2+1/3-1/4+...-1/2n
以下是C语言求解该数列前n项和的代码:
```c
#include <stdio.h>
void fun(float *sn, int n);
int main() {
int m, n;
float s;
printf("请输入测试实例的个数m:");
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("请输入正整数n:");
scanf("%d", &n);
fun(&s, n);
printf("该多项式的前%d项和为:%f\n", n, s);
}
return 0;
}
void fun(float *sn, int n) {
float s = 0.0, w, f = -1.0;
for (int i = 0; i <= n; i++) {
f = (-1) * f;
w = f / (2 * i + 1);
s += w;
}
*sn = s;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)