Advertisement
Guest User

P11916 - Approximation of e

a guest
Nov 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double factorial(double n) {
  5.   if (n == 0) return 1;
  6.   else return factorial(n - 1) * n;
  7. }
  8.  
  9. int main() {
  10.   cout.setf(ios::fixed);
  11.   cout.precision(10);
  12.  
  13.   double n;
  14.    while (cin >> n) {
  15.      double total = 0;
  16.      for (int i = 0; i < n; ++i) {
  17.        total += (1/factorial(i));
  18.      }
  19.      cout << "With " << int(n) << " term(s) we get " << total << '.' << endl;
  20.    }
  21.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement