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

(100 pts) Write a program that implements the following functions. long factoria

ID: 642519 • Letter: #

Question


(100 pts) Write a program that implements the following functions. long factorial(int n) double exponent(double x, int n) The functions implemented should follow below guidelines Factorial: Computes n! = n x (n - 1) x ? x 1 Exponent: Computes the sum of first n terms of e^x using the following approximation. f(x, y) = e^x = Read the value of n and x from the user and compute the first n terms of e^x using the function exponent. Print the result returned by the function and compare it with the value obtained by calling the math library function exp. When you increase the value of n your result should get closer to the result of exp. Sample execution of the program is given below Enter n and x 20 2.1 Approximation = 8.1753222282 Exact = 8.1661699126

Explanation / Answer

a)
#include <iostream>
#include<stdio.h>
using namespace std;

double exponential(double x,int n)
{
    float sum = 1.0f; // initialize sum of series
    for (int i = n - 1; i > 0; --i )
        sum = 1 + x * sum / i;
    return sum;
}
int main()
{
    int n = 10;
    double x = 1.0f;
    cout<<"e^x = "<<exponential(x,n);
    return 0;
}

b)
#include <iostream>
#include<stdio.h>
using namespace std;

long factorial(int n)
{
       if(n==1)
       return 1;
       else
       return n*factorial(n-1);
}
int main()
{
    int n = 5;
    cout<<"n! = "<<factorial(n);
    return 0;
}