/*********************************************
Approximating e by series expansion
Using the formula:
e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ...
Write a program that takes a number of terms
from the user and prints out the approximation
of e given by that number of terms
*********************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int factorial(int);
/*********************************************
** main() function
*********************************************/
int main()
{
// Get n, the number of terms, from user
int n;
cout << "We'll compute e by series expansion\n"
<< "How many terms would you like: ";
cin >> n;
// Compute and sum n terms of series
double e = 0;
for(int i = 0; i < n; i++)
e = e + 1/double(factorial(i));
// Print approximation to screen
cout << setprecision(20)
<< "e is approximately " << e << endl;
return 0;
}
/*********************************************
** factorial - Does what you think!
*********************************************/
int factorial(int x)
{
int f = 1;
for(int i = 2; i <= x; i++)
f = f*i;
return f;
}