//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
1 #include<stdio.h> 2 #include<math.h> 3 void main(){ 4 int s=1; 5 double n=1.0,t=1.0,pi=0; 6 while(fabs(t)>1e-7){ //fabs(double), abs(int) 7 pi=pi+t; 8 n=n+2; 9 s=-s; //实现正负交替 10 t=s/n; 11 } 12 pi=pi*4; 13 printf("π =%10.6lf ",pi); 14 }
结果为: