Background

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.

 

 

Example 

 

Output

 

 

n e

- -----------

0 1

1 2

2 2.5

3 2.666666667

4 2.708333333 

 1 #include<stdio.h>
 2 double fun(int i)
 3 {
 4     double re=2.5,to=6;
 5     int n;
 6     for(n=3;n<=i;n++)
 7     {
 8         re+=1/to;
 9         to*=(n+1);
10     }
11     return re;
12 }
13 
14 int main()
15 {
16     int i; 
17     printf("n  e\n");
18     printf("------\n");
19     printf("0  1\n1  2\n2  2.5\n");
20     for(i=3;i<=9;i++)
21     printf("%d  %.9lf\n",i,fun(i));
22     return 0;
23 }
View Code

 

posted on 2013-09-25 18:32  一颗向上的草莓  阅读(164)  评论(0编辑  收藏  举报