factorials

Question: The value of euler's number, e, can be approximated b using this formula:

e = 1+ 1/1! + 1/2! + 1/3! + 1/4! + 1/5!...

Using this formula, write a c++ program that approximates the value of e, using a while loop that terminates when the difference between two successive approximations is less than 10e-9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main()
{

//Problem #9

double x = 2.78;
double fact = 1;
double i = 1;
double myexp = 1;
while(fabs(exp(x)-myexp)>=10e-9)
                                {
                                fact =fact*i;
                                myexp=myexp+pow(x,i)/fact;
                                i=i+1;
                                cout<<fact<<"              "<<myexp<<endl;
                                }
system("PAUSE");

return 0;
                                
}


This is what I have written and its kinda hard for me to verify my results. Does this look correct to you guys though? I think I have the question answered... just need a second opinion.
Topic archived. No new replies allowed.