例4-1 格雷戈里公式

例4-1 格雷戈里公式

程序核心——while语句

程序

#include<stdio.h>
#include<math.h>
int main()
{
	int denominator,flag;
	double item,pi;
	
	flag=1;
	denominator=1;
	item=1.0;
	pi=0;
	while(fabs(item)>=0.0001)
	{
		item=flag*1.0/denominator;
		pi=pi+item;
		flag=-flag; 
		denominator+=2;
	}
	pi=pi*4;
	printf("*pi=%.4f\n",pi); 
 } 

结果

*pi=3.1418

--------------------------------
Process exited after 0.7024 seconds with return value 0
请按任意键继续. . .

分析

重点:循环语句while

posted on 2019-04-05 21:50  凯*凯  阅读(882)  评论(0编辑  收藏  举报

导航