Quiz 4

Create a function called euler_calc with a single parameter precision. The value of precision is used to determine when to stop calculating. Your calculation will stop when the two consecutive values estimating e differ by less than precision

 

//CODE

#include <iostream>
#include <iomanip>
using namespace std;

double factorial(int n){
double fact = 1;
for (int i = 1; i <= n; i++){
if (n==0){
return 1;
}
else {
fact = fact * i;
}
}
return fact;
}
float euler_calc (float ac){
float e = 1.0;
for (int i=1; i<999; i++){
e = e + 1/(factorial(i));
}
cout<< fixed <<setprecision(ac) << e;
return e;
}
int main(){
float ac;
cout<<“How accurate you want the result? “;
cin>>ac;
cout<<“Here is your number: “<<endl;
euler_calc(ac);
cout<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.49.28.png

CC BY-SA 4.0 Quiz 4 by everolivares is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.