C语言计算1-1/2+1/3-1/4……+1/99-1/100保留八位小数
时间: 2023-12-04 09:05:36 浏览: 111
以下是C语言计算1-1/2+1/3-1/4……+1/99-1/100保留八位小数的代码:
```c
#include <stdio.h>
int main() {
double sum = 0.0;
int sign = 1;
for (int i = 1; i <= 100; i++) {
sum += sign * 1.0 / i;
sign = -sign;
}
printf("%.8f", sum);
return 0;
}
```
阅读全文