CALCULATING e
¡Hello!
I found out how to show the exactly number of decimals that I wanted at:
You can download my code at: https://drive.google.com/a/itesm.mx/file/d/1bFeOYSSUGlc4pWENeNONjvTAQRDKcZhb/view?usp=sharing
or you can see my code here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//MarielCisneros | |
//TC1017 | |
#include <iostream> | |
#include <math.h> | |
using namespace std; | |
double factorial (int number){ | |
if (number==0){ | |
return 1; | |
} else { | |
return number*factorial(number-1); | |
} | |
} | |
double calculating_e(double precision){ | |
double e=0.0,oldE; | |
int i=0; | |
do{ | |
oldE=e; //This is only to compare with precision and finish the cycle | |
e+= (1/factorial(i)); | |
i=i+1; | |
} | |
while(e-oldE>precision); | |
return e; | |
} | |
int main (){ | |
double precision; | |
int n; | |
cout<< "Ingresa la precisión que quieres que contenga e." << endl; | |
cin>> precision; | |
cout<< "Ingresa el número de decimales que quieres ver."<< endl; | |
cin>> n; | |
double euler= calculating_e(precision); | |
cout.precision(n); //Here you tell the program how many decimals has to show | |
cout<<"El valor de e (euler) es " <<fixed<<euler<< endl; //if you don't use "using namespace std" at the beginnig, you must put std::fixed | |
return 0; | |
} |