#include "stdio.h" void main() { double e = 1.0; int n = 1; for (;(1.0/(double)jc(n)) >= (1e-7);n++) { e += 1.0/(double)jc(n); } printf("%lf\n",e); } int jc(int a) { int re = 1.0; for (;a > 0;a--) { re *= a; } return re; }
直接法:
#include "stdio.h" void main() { double e = 1.0; //定義e的初始值 double jc = 1.0; //階乘 int n = 1; for (;1/jc >= 1e-7;n++) { jc *= n; //求n的階乘 e += 1/jc; } printf("e的值為:%lf\n",e); }
運行結果: