c++ - Calculating mathematical constant e using while loop -


i doing task in book asks me calculate mathematical constant e using while loop. managed easily, having troubles calculating e^x, whereas user inputs x , degree of accuracy. code used computing e is:

#include <iostream> #include <iomanip> using namespace std;  int main() { int degreeofaccuracy, x = 1; long double e = 1; cout << "enter degree of accuracy of mathimatical constant e: "; cin >> degreeofaccuracy; while (x <= degreeofaccuracy) {       int conter = x;       int intial = x;       long double number = x;       int counter = 1;       while (conter > 1)       {              number = number*(intial-counter);              counter++;              conter--;       }       e += (1/number);       x++; } cout << endl << "the mathematical constantr e is: "       << setprecision(degreeofaccuracy) << fixed << e << endl; system("pause"); return 0; } 

however, when tried e^x following code returned wrong value:

#include <iostream> #include <iomanip> using namespace std;   int main() { int degreeofaccuracy, x = 1, exponent; long double e = 1; cout << "enter degree of accuracy of mathimatical constant e: "; cin >> degreeofaccuracy; cout << "enter number of wish raise e: "; cin >> exponent; int temp = exponent; while (x <= degreeofaccuracy) {       exponent = temp;       int conter = x;       int intial = x;       long double number = x;       int counter = 1;       while (conter > 1)       {              number = number*(intial-counter);              counter++;              conter--;       }       int counterr = 1;       while (counterr < x)       {             exponent *= exponent;             counterr++;       }       e += (exponent/number);       x++; } cout << endl << "the mathematical constantr e is: " << setprecision(degreeofaccuracy) << fixed << e << endl; system("pause"); return 0; } 

any ideas calculations went wrong?

this line:

exponent *= exponent; 

is wrong. should be:

exponent *= temp; 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -