For any integer n > 0, n!(n factorial) is defined as the product
n * n - 1 * n − 2 … * 2 * 1.
And 0! is defined to be 1
Create function that takes n as input and computes then returns the accurate value for:
n!= n * n - 1 * n − 2 … * 2 * 1
prompt the user to enter an integer n, call functions to compute the accurate value for n! and then display the results. The message displaying the result should look something like this:
5! is 120 accurately.
Test the program on nonnegative integers less than 8, and also on integers greater than 8. At what value does the approximation fail to generate accurate results? Generate a message that reports if the approximation is not valid anymore.
TIP: Be careful with the type conversions. Be sure to use a named constant for PI, and use the approximation 3.14159265
functions must have prototype
Programming Language is C
#include <stdio.h>
#include <limits.h>
int factorial(int n)
{
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
if (fact > INT_MAX)
printf("Factorial is not accurate.');
else
printf("Factorial of %d = %llu", n, fact);
}
}
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
factorial(n);
factorial(-9);
factorial(10);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.