For any integer n > 0, n! is defined as the product n x n - 1 x n - 2 ... x 2 x
ID: 3642132 • Letter: F
Question
For any integer n > 0, n! is defined as the product n x n - 1 x n - 2 ... x 2 x 1. O! is defined to be 1. It is sometimes useful to have =- closed-form definition instead; for this purpose, an approximati can be used. R.W. Gosper proposed the following such approxima- tion formula:
Programming Projects
Create a program that prompts the user to enter an integer n, uses
Gasper's formula to approximate n!, and then displays the result. The
message displaying the result should look something like this:5! equals approximately 119.97003
Your program will be easier to debug if you use some intermediate val-
ues instead of trying to compute the result in a single expression. If you
are not getting the correct results, then you can compare the results of
your intermediate values to what you get when you do the calculations
by hand. Use at least two intermediate variables-one for 2n + 1/3 and one
for sqrt(2n+ 1/3 )pi. Display each of these intermediate values to simplify
debugging. Be sure to use a named constant for PI, and use the approxi-
mation 3.14159265. Test the program on non-negative integers less than 8.
here's my code
#include <stdio.h>
#include <math.h>
#define pi 3.14159265
int main(void){
int n;
double x;
printf("Enter an integer >");
scanf("%d", &n);
x=((pow(n,n)*exp(-n)*sqrt((2.0*n+(1.0/3.0)*pi))
printf("%d! equals approximately %.2f ",n,x);
return(0);
}
Explanation / Answer
I think the only error is a missing semicolon. replace x=((pow(n,n)*exp(-n)*sqrt((2.0*n+(1.0/3.0)*pi)) by x=((pow(n,n)*exp(-n)*sqrt((2.0*n+(1.0/3.0)*pi));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.