公式e=1+1/1!+1/2!+1/3!+......,求 e 的近似值,當下一項的值精度小於10的-6次方時停止累加運算。

#include<stdio.h>
//函數功能:計算求和變量e,精度爲f;
double fun(double f)
{
        double e=1.0;
        double jc=1;//求階乘,並存入jc中
        /**********Program**********/
        int i = 1;//用於循環和遞歸增加 
       while(1/jc >= f){
       	e = e + 1/jc;
       	i++;
       	
       	jc *= i;//存放階乘 1! 2! 3! 4! 5!.... 
       	
       }

 
    /**********  End  **********/
        return e;
}
 
int main()
{
         printf("e=%lf\n",fun(10e-6));  
         return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章