Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let n be a nonnegative integer. The factotial of n, written n! Is defined by 0!=

ID: 3818929 • Letter: L

Question

Let n be a nonnegative integer. The factotial of n, written n! Is defined by 0!=1,n!=1x2x3...n>=1. For example,4!=1x2x3x4=24. Write a program in C++ language that prompts the user to enter a nonnegative integer and outputs the factorial of the number, then find the approximate value of the number e using the read input as n in the approximating equation below

e=2+1/2+1/3+...+1/n!, (e is a irrational number=2.718281827.. used in exponential growth and decay problems.

Run the output for at least n=100,n=50,n=200.

Explanation / Answer

#include <iostream>

using namespace std;

double factorial(int n) {

   double fact = 1;

   for(int i = 1; i<= n; i++)

      fact = fact * i;

   return fact;

}


double e(int limit) {

   double val;

   double sum = 0;

   for (int n = limit; n>1; n--) { //reverse loop to increase precision of sum.

      val = 1.0/factorial(n);

      sum = sum + val;

   }

   sum = sum + 2;

   return sum;

}


int main() {

   cout.precision(17);

   cout<<"Value of e(50) is "<< fixed << e(50)   << endl;

   cout<<"Value of e(100) is "<< fixed << e(100) << endl;

   cout<<"Value of e(200) is "<< fixed << e(200) << endl;

   return 0;

}

Output:
./a.out

Value of e(50) is 2.71828182845904509

Value of e(100) is 2.71828182845904509

Value of e(200) is 2.71828182845904509

I hope this solves your problem. I haven't put any comments because I believe that the code is easy for you. Incase you need assistance on the code please comment below.