π=4 (1-1/3+1/5-1/7+……+1/99)的近似值
时间: 2024-01-29 17:02:50 浏览: 104
π的近似值.java
以下是两种计算π的近似值的方法:
1. 使用for循环计算π的近似值:
```c
double sum = 0;
int i;
for(i = 0; ; i++) {
double term = 1.0 / (i*2+1);
if(i % 2 == 0)
sum += term;
else
sum -= term;
if(term < 1e-6)
break;
}
double pi = 4 * sum;
printf("%.6f\n", pi);
```
2. 使用do-while循环计算π的近似值:
```c
double term;
double sum = 1.0;
int i = 1;
do {
term = 1.0 / (i*2+1);
if(i % 2 == 0)
sum += term;
else
sum -= term;
i++;
} while(term > 1e-6);
double pi = 4 * sum;
printf("%.6f\n", pi);
```
阅读全文