求π(派)的近似值
#include <stdio.h> #include <math.h> int main() { int sign,n; float p,i,t; i=1.0; sign=1; n=0; p=0.0; t=1.0; while(fabs(t)>=1e-6) { sign=-sign; i=i+2; p=p+t; t=sign/i; n=n+1; } p=p*4; printf("%f\n",p);//π的近似值 printf("%d\n",n);//执行次数 return 0; }