Problem Description
A simple mathematical formula for e is
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
该题注意输出格式,即当n=0时,输出1,而不是1.000000000。类似的n=1和n=2时也是这样。
#include<stdio.h>
void calculate_fac();
int fact[10];
int main()
{
int i,j;
printf("n e\n- -----------\n");
printf("0 1\n1 2\n2 2.5\n");
calculate_fac();
for(i=3;i<=9;i++)
{
double e=0;
printf("%d ",i);
for(j=0;j<=i;j++)
{
e+=1.0/fact[j];
}
printf("%.9lf\n",e);
}
return 0;
}
void calculate_fac()
{
int i;
fact[0]=1;
for(i=1;i<=9;i++)
{
fact[i]=i*fact[i-1];
}
}