Tag Archives: #recursivestuff

WSQ 14

 

The constant “e” was named after Leonhard Euler. This constant represent the base of all natural logarithms and due to the fact that “e” is an irasional number, it has dousands of decimals of acuracy. 

One way of calculating the value of “e” is using the formula: 1 + 1 + 1/2 + 1/6 + 1/24 + 1/120 = 2.718055556

(Euler’s number in math is fun, 2014) http://www.mathsisfun.com/numbers/e-eulers-number.html

Here is an example of how to calculate “e” using recursion and factorial:

note: In the program the user needs to enter the amount of decimals that will be printed. 

<iostream>

 

using namespace std;

 

int fact(int a){

    if(  a == 0) {

    return 1;

   }else {

   a= a*fact(a-1);

  }

 }

double calculate_e(){

  int precision;

  double e;

  int a;

  double sum=0;

  cout<< “specify the number of decimal points “<<endl;

  cin>> (precision);

  for( a=0; a<=precision; a++){

   e= 1.0/fact(a);

   sum = sum+e;

  }

cout <<” e: “<<sum;

}

 int main(){

   int precision;

 calculate_e();

 

 return 0;

 

}