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

(C++ microsoft visual studio) a) The factorial function n! is defined by n!=1?2?

ID: 3741154 • Letter: #

Question

(C++ microsoft visual studio)

a) The factorial function n! is defined by n!=1?2?…?n for positive integer values of n>=1. Implement a functions computes n! b) The number e is given by e=2.718281827…. The value of e can be approximated using the following expression:

2 + (1/2!) + (1/3!) + ... + (1/n!)

where n is a positive integer. Write a program that prompts the user for the value of n. The program then uses the formula above to approximate the value of e. Test your program for n = 4, 8, 10, and 12.

Explanation / Answer

Code for Question A and B is as follows:

#include <iostream>
using namespace std;

// Function findFactorial gets number and returns its factorial
long findFactorial(long num)
{
unsigned long factorial = 1;
for(int i = 1; i <= num; ++i)
{
factorial = factorial * i;
}
  
return factorial;
}

int main()
{
unsigned int num; // variable declaration
unsigned long fact;
double e = 1.0;
  
cout << "Enter positive integer : ";
cin >> num; // reading number
  
fact = findFactorial(num);
cout << "Factorial of " << num << " = " << fact << endl; // Answer of Question A)
  
for(int j = 1; j <= num; ++j)
{
e = e + (1.0/findFactorial(j));
}
  
cout << "value of e for " << num << " = " << e; // Answer of Question B)
  
return 0;
}

***************** Output********************

When n =4,

Enter positive integer : 4                                                                                         

Factorial of 4 = 24                                                                                                

value of e for 4 = 2.70833

When n =8,

Enter positive integer : 8                                                                                         

Factorial of 8 = 40320                                                                                             

value of e for 8 = 2.71828

When n =10,

Enter positive integer : 10                                                                                        

Factorial of 10 = 3628800                                                                                          

value of e for 10 = 2.71828

When n =12,

Enter positive integer : 12                                                                                        

Factorial of 12 = 479001600                                                                                        

value of e for 12 = 2.71828