首页 > 代码库 > 1012 u Calculate e
1012 u Calculate e
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
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。