3.用台劳级数求e的近似值,直到最后一项小于10-6
e=1+1/1!+1/2!+1/3!+ ……
long fun(long n)
{
while(n>=0)
{
if(n==0)return 1;
else return n*fun(n-1);
}
}
main()
{
double d,e=1.0;
long i=1;
do
{
e+=(double)(1.0/fun(i));
d=(double)(1.0/fun(i));
i++;
}
while(d>10e-6);
printf("%lf\n",e);
getch();
}
e=1+1/1!+1/2!+1/3!+ ……
long fun(long n)
{
while(n>=0)
{
if(n==0)return 1;
else return n*fun(n-1);
}
}
main()
{
double d,e=1.0;
long i=1;
do
{
e+=(double)(1.0/fun(i));
d=(double)(1.0/fun(i));
i++;
}
while(d>10e-6);
printf("%lf\n",e);
getch();
}
新王登基,血流成河!