Problem Description
A simple mathematical formula for e is
u Calculate e
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
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int i,sum;
    double a[100];
    sum=1;
    for(i=0;i<=9;i++)
    {
           if(i==0)
               a[i]=1;
           else
        {
                  sum=sum*i;
            a[i]=a[i-1]+1.0/sum;
        }
    }
    printf("n e\n- -----------\n");
    for(i=0;i<10;i++)
    {
        cout<<i<<" ";
        if(i<2)
            printf("%.0lf\n",a[i]);//小数点后保留0位有效数值
        else if(i==2)
            printf("%.1lf\n",a[i]);//小数点后保留1位有效数值
        else
            printf("%.09lf\n",a[i]);//小数点后保留9位有效数值
    }
    return 0;
}

相关文章:

  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2021-04-22
  • 2021-09-04
  • 2021-11-12
  • 2021-07-09
  • 2021-09-25
  • 2021-08-07
相关资源
相似解决方案