The factorial of a nonnegative integer n is written n! (pronounced \"n factorial
ID: 3789233 • Letter: T
Question
The factorial of a nonnegative integer n is written n! (pronounced "n factorial")* and is defined as follows: n! = n middot (n - 1) middot (n- 2) middot...middot 1 (for values of n greater than 1) and n! = 1 (for n = 0 or n = 1). For example, 5! = 5 middot 4 middot 3 middot 2 middot 1, which is 120. Use while statements in each of the following: a) Write a program that reads a nonnegative integer and computes and prints its factorial. b) Write a program that estimates the value of the mathematical constant e by using the^formula: e = 1 + 1/1! + 1/2! + 1/3! + ... Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation). c) Write a program that computes the value of ex by using the formula e^x = 1 + x/1! + x^2/2! + x^3/3! + ... Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).Explanation / Answer
a) void main ()
{ int n,fact=1,i=1;
clrscr();
cout<<" enter the first number";
cin>>n;
//loop used to find factorial
while(i<=n)
{ fact=fact*i;
i++;
}
cout<<" the factorial of"<<n<<"is"<<fact;
getch();
}
int factorial(int);
b)
void main()
{
float sume=1;
int i=1,n;
cout<<“ Enter the limit: “;
cin>>n;
while(i<=n)
{
sume=sume+i/factorial(i);
i++;
}
cout<<“ e ="sume);
}
int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
return fact;
}
c)
float factorial(int n)
{
float f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
float series(int n,int x)
{
float h=1;
for(int i=1;i<=n;i++)
h=h*x;
return h;
}
int main()
{
clrscr();
int n,x,i;
float s=1;
cout<<"Enter the value for 'n' & 'x': ";
cin>>n>>x;
for(i=1;i<=n;i++)
s=s+series(i,x)/factorial(i);
cout<<"e^x= "<<s;
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.