program will approximate the value of e...when x=5.7.. but i am not getting the result right . could anybody help me and show my mistake please...
x=1+1/1!+1/2!+1/3!+1/4!+1/5!+.......
when the difference between two successive approximations is less tan 10e-9
Without that change I get an error C2668. BTW: If the compiler would tolerate the wrong statement, it would even work, since e is approached from below...
But I had to make some more changes to your program to actually compile and correctly calculate the (approximate) value of e. And only that way it matches the approximation formula from post #1. Here is the corrected version, with the changes marked in red:
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
double /* x, */myexp1,myexp2,expx,fact,i;
fact=1;
ofstream output("e:result12.dat");
cout<<" ....."<<endl;
output<<" ..... "<<endl;
// x=5.7; // That's not needed anymore
myexp1=1;
myexp2=1;
i=2;
expx=myexp1+myexp2;
cout << setprecision(12); // In order to see all changes up to the last iteration
output << setprecision(12); // ditto
while (fabs(expx-myexp1) >= 10e-9)
{
fact=fact*i;
myexp1=expx;
expx=expx+1.0/fact;
i=i+1;
cout<< "..... " <<"i= "<<expx<<endl;
output<< ".... " <<"i= "<<expx<<endl;
}
system("PAUSE");
return 0;
}
I hope I didn't miss any change.
... And I hope this wasn't homework I now have done for you...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Bookmarks