Hello,
I'm writing a program to calculate the value of e^x using taylor series expansion as my algorithm( e^x= 1 + x + (x^2)/(2!) + (x^3)/(3!)+......(x^n)/(n!)) . I think I'm going wrong somewhere within the while loop. Can you please help me out?
Thanks
Code:#include <iostream> #include <cmath> #include <iomanip> int main() { double xvalue, factorial, ex, nthterm, factnum, numerator, sumofterms; double xcount = 1; numerator = pow(xvalue,factnum); factorial *= (factnum - xcount); nthterm = (numerator / factorial); cout<< "This program will calculate the value\n" << "of e^x using the Taylor Series Expansion Method\n" << endl; cout<< "Please enter a value for 'x' for which you need to calculte" << endl; cin>> xvalue; cout<< endl; while(nthterm > 10E-6) { while(xcount != factnum) { factorial *= (factnum - xcount); factnum++; } sumofterms += nthterm; cout<< "This program will calculate the value\n" << "of e^x using the Taylor Series Expansion Method\n" << endl; cout<< "Please enter a value for 'x' for which you need to calculate" << endl; cin>> xvalue; cout<< endl; while(nthterm > 10E-6) { while(xcount != factnum) { factorial *= (factnum - xcount); factnum++; } sumofterms += nthterm; } ex = 1+xvalue+sumofterms; cout<< ex; cout<< endl; return 0; }
Code tags added by Kermi3



LinkBack URL
About LinkBacks



