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

Write a program that implements the following functions. long factorial(int n) d

ID: 672575 • Letter: W

Question

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! = nx(n - 1) x ... x 1 Exponent: Computes the sum of first n terms of ex using the following approximation. Read the value of n and x from the user and compute the first n terms of ex 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

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

using namespace std;


long factorial(int n){

long fn;

if(n==0){return 1;}

else{fn=(factorial(n-1))*n; return fn;}

}


double exponent(double x, int n){

double en;

if(n==0){return 1.0;}

else{en=(exponent(x,(n-1)))*x; return en;}

}

int main() {

int n; double x,sum=0;

cout<<"Enter n and x"<<endl;

cin>>n>>x;

for(int i=0;i<=n;i++){

sum=sum+exponent(x,i)/(double)factorial(i);

}

cout.precision(11);

cout<<"Approximation = "<<(double)sum<<endl;

cout<<"Exact = "<<(double)exp(x)<<endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote