I need the CORRECT code for this program. Also, PLEASE answer the question in a
ID: 3811544 • Letter: I
Question
I need the CORRECT code for this program. Also, PLEASE answer the question in a sentence. Im attaching the sample code with the description. Thank you.
Description: The standard C library does not have a function that computes the factorial of a positive integer. Write a C pro- gram that calculates the exact and approximate value of the factorial. You program should include 4 functions, one for returning the exact value of factorial if the integer is S 14, two for returning the approximate value of factorial using Stirling's formulas given below and one for clearing the input buffer (see the algorithm) 2 n n'" e 2 Tt n 1 12 n A sample interaction and output of the program is given on the next page. Your program should prompt the user for a positive integer and check its validity. If the input is not a positive integer, the program should print a message and invite the user to input a positive integer again. If the positive integer is S 14, the program should print the first table shown in sample interaction. This should include entries for all integers less than or equal to the number read as input. If the integer is 14, it should print the second table. This should include entries for l factorial, all multiples of 5 less than or equal to the number read as input and an entry for the number read as input. The output of your program should match the given format. The program should terminate when 'n' is entered as input. See the sample interaction. After completing and testing your program, answer the following question and write your answer as comment at the end of your program. Question: (a) How would you find the exact factorial for a large integer? Briefly explain.Explanation / Answer
//main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*--------------------
Preprocessor Section
---------------------*/
#define PI 3.141592653589793
int factorial( int n )
{
if( n == 1 || n == 0 ){
return 1;
} else {
return factorial( n - 1 ) * n;
}
}
double stirlingsApproximation1( int n )
{
return pow( n, n ) * exp( -n ) * sqrt( 2.0 * PI * n );
}
double stirlingsApproximation2( int n )
{
return ( pow( n, n ) * exp( -n ) * sqrt( 2.0 * PI * n ) ) *
( 1.0 + ( 1.0 / (double)( 12 * n ) ) );
}
void printHeaderWithFactorial( )
{
printf( "Number Factorial Approximation1 Approximation2 " );
printf( "-------------------------------------------------------"
"-------- ");
}
void printApproximationHeader( )
{
printf( "Number Approximation1 Approximation2 " );
printf( "------------------------------------------------------ ");
}
void printFactRow( int i, int fact, double appx1, double appx2 ){
printf( "%4d %9d %12.6e %12.6e ", i, fact, appx1, appx2 );
}
void printAppxRow( int i, double appx1, double appx2 ){
printf( "%4d %12.6e %12.6e ", i, appx1, appx2 );
}
void inputRequest( ){
printf( " Please enter a nonnegative integer: " );
}
void invalidInputError( ){
printf( " The factorial is undefined for negative integers. " );
}
int main( )
{
int input;
inputRequest();
for( scanf( "%d", &input ); input != 99999; scanf( "%d", &input ) ){
if( input <= 0 ) {
invalidInputError( );
} else {
int i;
if( input <= 12 ){
printHeaderWithFactorial();
for( i = 1; i <= input; i++ ){
printFactRow( i, factorial( i ), stirlingsApproximation1( i ),
stirlingsApproximation2( i ) );
}
} else {
printApproximationHeader();
printAppxRow( 1, stirlingsApproximation1( 1 ),
stirlingsApproximation2( 1 ) );
for( i = 5; i < input; i+=5 ){
printAppxRow( i, stirlingsApproximation1( i ),
stirlingsApproximation2( i ) );
}
printAppxRow( input, stirlingsApproximation1( input ),
stirlingsApproximation2( input ) );
}
}
inputRequest();
}
return (EXIT_SUCCESS);
}
===========================================================