Calculate Factorial in Java : (Factorial) The factorial of a non negative integer n is written as n! (pronounced “n factorial”) and is defined as follows:
n! = n · (n – 1) · (n – 2) · … · 1 (for values of n greater than or equal to 1)
and
n! = 1 (for n = 0)
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
a) Write a program that reads a non negative integer and computes and prints its factorial.
Do not use scientific notation for displaying the result.
b) Write a program that estimates the value of mathematical constant e by using the formula:
e = 1 + (1/1!) + (1/2!) + (1/3!) …
Calculate Factorial in Java
/*
* Filename: Factorial.java
*
* Description: Exericse 4.37 - Factorial
*
* NOTE: I have no idea if b or c are correct or not as I am
* not mathematically inclined.
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;
public class Factorial{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// A
System.out.print("Enter a number to compute its factorial: ");
int f = sc.nextInt();
System.out.printf("The factorial of %d = %d\n", f, getFactorial(f));
// B
System.out.print("Enter the number of times to calculate e: ");
int n = sc.nextInt();
System.out.printf("e = %.2f\n", getConstantE(n));
// C
System.out.print("Enter the number of times to calculate e^x: ");
n = sc.nextInt();
System.out.print("Enter x: ");
int x = sc.nextInt();
System.out.printf("e^%d %d times = %.2f\n", n, n, getValueE(n, x));
}
// A - compute and return factorial
// x! = x-1 * x-1 * x-1 * x-1 ...
private static int getFactorial(int x){
int factorial = x;
if(x > 1)
x--;
while(x > 0){
factorial *= x;
x--;
}
return factorial;
}
// B - compute mathematical constant e
// e = 1 + 1/1! + 1/2! + 1/3...
private static double getConstantE(int x){
double e = 1.0;
for(int i=1; i<=x; i++){
e += 1.0/getFactorial(i);
}
return e;
}
// C - compute value of e^x.
// e^x = 1 + x/1! + x^2/2! + x^3/3!...
private static double getValueE(int n, int x){
double e = 1.0;
for(int i=1; i<=n; i++){
e += x/getFactorial(i);
x = (int)Math.pow(x, i);
}
return e;
}
}