计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和。 用C编写
时间: 2023-10-02 14:08:03 浏览: 77
计算前N项之和(c语言)
#include <stdio.h>
int main() {
int n;
printf("请输入要计算的项数:");
scanf("%d", &n);
double sum = 0;
int sign = 1, numerator = 1, denominator = 3;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
sum += sign * numerator / (double)denominator;
} else {
sum -= sign * numerator / (double)denominator;
}
sign *= -1;
numerator++;
denominator += 2;
}
printf("前%d项之和为:%lf\n", n, sum);
return 0;
}
阅读全文