Write a program that implements the following functions. The functions implement
ID: 3769508 • Letter: W
Question
Write a program that implements the following functions. The functions implemented should follow below guidelines Factorial: Computes n! = n x (n - 1) x ... x 1 long factorial(int n) double exponent(double x, int n) Exponent: Computes the sum of first n terms of e^x using the following approximation. 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.1661699126Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
long factorial(int n)
{
long f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
double exponent(double x,int n)
{
return pow(x, n);
}
int main()
{
int n,x,i;
float s=1;
cout<<"Enter n and x: ";
cin>>n>>x;
for(i=1;i<=n;i++)
s=s+exponent(x, i)/factorial(i);
cout<<"Approximation= "<<s<<" ";
cout<<"Exact= "<<exp(n)<<" ";
cout<<" ";
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.