#include <stdio.h>
#include <math.h>int main() {int fm = 1;//分母double sign = 1;//正负号double fzs = 1;//分子式double sum = 0;while (fabs(fzs) >= 0.000001) {sum += fzs;sign *= -1; //变换正负号fm += 2; //分母3、5、7、9...增长fzs = sign / fm;//分子式}printf("π的近似值为%.5lf\n", sum * 4);
}
运行结果:
本章C语言经典例题合集:http://t.csdnimg.cn/FK0Qg