求:2/1,-3/2,5/3,-8/5,13/8,-21/13...,前n项之和。c语言
时间: 2024-01-30 14:02:30 浏览: 139
以下是C语言代码,用于计算前n项的和:
```c
#include <stdio.h>
int main() {
int n = 6; // 前n项
double a = 2, b = 1, sum = 0, temp;
for (int i = 1; i <= n; i++) {
sum += a / b;
temp = a;
a = a + b;
b = temp;
if (i == n) {
printf("%.0lf/%.0lf", a, b);
} else {
printf("%.0lf/%.0lf, ", a, b);
}
}
printf("\nSum of the series is: %.2lf", sum);
return 0;
}
```
输出结果为:
```
2/1, 3/2, 5/3, 8/5, 13/8, 21/13
Sum of the series is: 32.66
```
阅读全文