1. 程式人生 > >1012 u Calculate e

1012 u Calculate e

blog calculate ear || cal can http where hat

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 5 2.716666667 6 2.718055556
7 2.718253968
8 2.718278770
9 2.718281526
 1 #include<stdio.h> 
 2 int jc(int n)
 3 {
 4     int i=1,s=1;
 5     if(n==1||n==0)
 6     return 1;
 7     while(i<=n)
 8     {
 9         s*=i;
10
i++; 11 } 12 return s; 13 } 14 int main() 15 { 16 double a[10]; 17 a[0]=1; 18 for(int i=1;i<=9;i++) 19 { 20 a[i]=1.0/jc(i)+a[i-1]; 21 } 22 printf("n e\n"); 23 printf("- -----------\n"); 24 for(int i=0;i<=9;i++) 25 { 26 if(i!=8) 27 printf("
%d %.10g\n",i,a[i]); 28 else 29 printf("%d %.9f\n",i,a[i]); 30 } 31 return 0; 32 }

1012 u Calculate e