Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The factorial of a number n is n * (n - 1) * (n - 2) * ... * 2 * 1. Stirling\'s

ID: 3542677 • Letter: T

Question

The factorial of a number n is n * (n - 1) * (n - 2) * ... * 2 * 1.  Stirling's formula approximates the factorial for large values of n:

nn * ?(2*?*n) / en

where ? = 3.14159265 and e = 2.718282

Write a Java application that inputs an integer value (but stores it into a double variable n), calculates the factorial of n using Stirling's formula, assigns the (rounded) result to a long integer variable, and then displays the result appropriately labeled.

Depending on the value of n, you should obtain one of these results

Because Stirling's formula is used to calculate the factorial of very large numbers, the factorial approachesLong.MAX_VALUE quickly.  If the factorial exceeds Long.MAX_VALUE, it causes an arithmetic overflow in the computer, in which case the program continues with a strange-looking integer result, perhaps a negative value.  Before you write the application, then, you first must write a small application that lets you determine, by trial and error, the largest value of n for which Java can compute a factorial using Stirling's formula.  After you've determined this value, you can write the application using nested if-statements that display different messages depending on the value of n.  If n is within the acceptable range, output the number and the result with an appropriate message.  If n is 0, write the message "The number is 0.  The factorial is 1."  If n is less than 0, write "The number is less than 0.  The factorial is undefined."  If the number is greater than the largest value of n for which Java can compute a factorial, write "The number is too large."

Hint: Don't compute Stirling's formula directly.  The values of nn and en can be huge, even in floating-point form.  Take the natural logarithm of the formula and manipulate it algebraically to work with more reasonable floating-point values.  If r is the result of these intermediate calculations, the final result is er.  Make use of the standard library methods Math.log andMath.exp.  These methods compute the natural logarithm and natural exponent, respectively.  You can see an example of manipulating Stirling's formula using natural logarithms in class.

Explanation / Answer

 import java.util.Scanner;   class Factorial {    public static void main(String args[])    {       int n, c, fact = 1;         System.out.println("Enter an integer to calculate it's factorial");       Scanner in = new Scanner(System.in);         n = in.nextInt();         if ( n < 0 )          System.out.println("Number should be non-negative.");       else       {          for ( c = 1 ; c <= n ; c++ )             fact = fact*c;            System.out.println("Factorial of "+n+" is = "+fact);       }    } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote