用c语言用while语句计算圆周率的近似值。(直到累加项的绝对值小于 0.00001 ) 公式 : pi /4 ≈1-1/3+1/5-1/7 .......
时间: 2023-09-19 20:09:22 浏览: 229
用c语言求圆周率
Sure, I'd love to help you with that! Here's the C code using while loop to approximate the value of pi:
```
#include<stdio.h>
int main()
{
double pi = 0.0;
double frac = 1.0;
int sign = 1;
double term = 1.0;
while(term > 0.00001 || term < -0.00001)
{
pi += term;
sign *= -1;
frac += 2.0;
term = sign / frac;
}
pi *= 4;
printf("The approximated value of pi: %f\n", pi);
return 0;
}
```
Hope this helps!
阅读全文