A useful approximation to the value of n! for large values of n is given by S n
ID: 3883886 • Letter: A
Question
A useful approximation to the value of n! for large values of n is given by Sn = sqrt(2pi)e-nnn+(1/2)
Develop a Java application in a class named FactorialApproximation that prompts for and reads one integer numeric amount that represents the value of n for n!. Use the approximation formula to calculate n!.
Requirements
· Use at least 2 named constants in your code
· Display a header for your application.
· Ask the user for the value of n.
· Restrict the values of n to whole numbers that are greater than 0 and less than 100. Any value that is outside this range should cause the program to end after an error message is printed.
· If the value of n is in the proper range, calculate the estimated value of n!
· Display the approximation of n! that results, with an appropriate label.
· If the value of n is equal to 5 or 7, compare the actual value of 5!, or 7!, to the value calculated by the formula. Then print the difference (actual value - calculated value) with an appropriate label.
· Display your name as programmer at the end.
· Use the Scanner class for input, and use System.out.print or System.out.println for output.
· Your program must use the single class FactorialApproximation and only one static void main method.
Your code must not use loops, return statements, System.exit(), arrays or collections
The equation is ((sqrt(2pi))(e^-n)(n^n+(1/2))
Explanation / Answer
Hi,
1. FactorialApproximation.java
import java.util.Scanner;
public class FactorialApproximation {
private static double PI = Math.PI;
private static double EXPO = Math.E;
public static void main(String[] args){
System.out.println("Header:Calculation of approximation to the value of n!");
try {
System.out.println("Please enter a number ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n>0 && n<100) {
int approxValue = 0;
//((sqrt(2pi))(e^-n)(n^n+(1/2))
// Calculate value using formula
// Math.sqrt(2*PI) is calculate sqr root of 2*PI
// Math.pow(EXPO, -n) is calculate power of e^-n
// Math.pow(n, n) is calculate power of n^n
approxValue = (int)((Math.sqrt(2*PI))*(Math.pow(EXPO, -n))*(Math.pow(n, n)+0.5));
System.out.println("Approximation value of "+n+"! is "+approxValue);
// check n equal to 5 then find difference
if (n == 5) {
int difference = 120 - approxValue;
System.out.println("The difference of actual value of 5! and calculated value(5!) is "+difference );
}
// check n equal to 7 then find difference
if (n == 7) {
int difference = 5040 - approxValue;
System.out.println("The difference of actual value of 7! and calculated value(7!) is "+difference );
}
System.out.print(" Programmer Name: ABC XYZ");
} else {
System.out.println(n+ " is not in range of 0 to 100");
}
} catch (Exception e){
System.out.println("Please enter whole numbers that are greater than 0 and less than 100");
}
}
}
Please change header and programmer name as per ypur requirement.
let me know if any change required.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.