C語言 用π/4=1-1/3+1/5-1/7+... 求π的近似值


//凱魯嘎吉 - 博客園 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\n",pi);
14 }

結果為:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM