c 语言求派的近似值
时间: 2024-01-25 21:10:13 浏览: 95
094 求π的近似值 C语言源代码文件
5星 · 资源好评率100%
以下是C语言求π的近似值的代码和流程图:
```c
#include <stdio.h>
#include <math.h>
int main() {
int sign = 1; // 正负1变换
double pi = 0.0, n = 1.0, term = 1.0; // 定义初始化π,等式右边的每一项
while (fabs(term) > 0.000001) {
pi = pi + term;
n = n + 2;
sign = -sign;
term = sign / n;
}
pi = pi * 4;
printf("pi=%10.8f\n", pi);
return 0;
}
```
流程图如下:
```
开始
sign = 1, pi = 0.0, n = 1.0, term = 1.0
while(fabs(term) > 0.000001) {
pi = pi + term;
n = n + 2;
sign = -sign;
term = sign / n;
}
pi = pi * 4;
输出 pi
结束
```
阅读全文