已知公式e = 1 + 1/1! + 1/2! + 1/3! + … +1/n!, 编程计算e的近似值,直到最后一项的绝对值小于1e-7时为止,输出e的值并统计累加的项数。要求:按顺序输出每一个e值,小数点后保留8位有效数字,输出格式形如:e = 2.66666667, count = 4(回车换行,count为累加的项数)
#include <stdio.h>
#include <math.h>
main()
{
int i=1,j=1,count=1,m;
float e=1.0,k;
if(count==1)
printf("e = %f,count = %d\n",e,count);
do{
j=i*j;
k=1.0/j;
e=e+k;
i++;
count++;
printf("e = %f,count = %d\n",e,count);
}while(k>1e-7);
return 0;
}