// eSeries.cpp
// Computes a series approximation of e.
// Series approximations to e, using formula:
//
//             1    1    1    1    1
//         e = -- + -- + -- + -- + -- + ...
//             0!   1!   2!   3!   4!
//
// Displays all of the first 12 approximations.

#include <iostream>
#include <iomanip>

int main()
{
   // Display table headings:
   cout << endl;
   cout << "e series approximations" << endl;
   cout << "-----------------------" << endl << endl;
   cout << " n    Sum to nth term" << endl;
   cout << "--    ---------------" << endl << endl;

   // Display table body, first row:
   double sum = 1;  // zeroth approximation
   double factorial = 1;
   cout << setw(2) << 0;   // to right-justify left column
   cout << "     " << sum << endl;   // right column

   // Display remainder of table body::
   for ( int n = 1; n <= 12; n++ )
   {
      // Calculate nth term and add to series:
      factorial = factorial * n;
      sum = sum + 1/factorial;

      // Display row of table, with right-justified left column:
      cout << setw(2) << n;   // to right-justify left column
      cout << "     " << sum << endl;   // right column
   }  // for n

   return 0;
}  // function main