C语言求π的近似值

#include<stdio.h>
#include<math.h>//使用了fabs()函数
int main()
{	
  int s;	
  double n,t,pi;	
  t = 1;	
  pi = 0;	
  n = 1.0;//作为分母	
  s = 1;//作为分子1,并调节±符号	
  while(fabs(t) > 1e-6)//判断t的绝对值是否大于10的-6次方	
  {		
   pi +=t;		
   n +=2;		
   s = -s;		
   t = s/n;	
   }	
   printf("%lf",4*pi);	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45824959/article/details/105058111
今日推荐