欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2020-12-15

程序员文章站 2024-01-29 10:29:34
...

pi的近似值计算

#include<stdio.h>
#include<math.h>
int main(void)
{
	double pi, sum = 0, term, sign = 1.0;
	int count = 0, n = 1;
	do 
	{
		term = sign / n ;
		sum = sum+term ;
		count++;
		sign = -sign ;
		n = n + 2 ;
	} while( fabs(term) >= 1e-4);
	pi = sum * 4 ;
	printf("pi = %f\ncount = %d \n",pi,count) ;
	return 0;
}

e的近似值计算

#include<stdio.h>
#include<math.h>
int main(void)
{
	int n=1,count=1; 
	double sum=1,term=1; 
	while(fabs(term)>=1e-5) 
	{ 
		term=term*(1.0/n); 
		sum=sum+term; 
		n++; 
		count++; 
	} 
	printf("sum=%f,count=%d\n",sum,count); 

	return 0;
}
相关标签: 笔记