--Originally published at TC1017 Programing Curse
In this assignment i had to estimate the mathematical constant e. I create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.
There is the code
#include <iostream>
#include <math.h>
using namespace std;
int getFactorial(int num){
if (num == 1 || num == 0)
{
return 1;
}
else
{
return num*getFactorial(num-1);
}
}
float euler_calc(float par){
float num = 0.0, result=0.0;
do{
result += 1.0/(getFactorial(num));
num = num + 1;
} while(num <= par);
return result;
}
int main(){
float par, result, num=0.0;
cout << “Give me the parameter ” << endl;
cin >> par;
cout << “The answer is ” << euler_calc(par) << endl;
return 0;
}
image taken from http://digg.com/video/eulers-constant-explained