The factorial program in Java

1. What is factorial?

For a positive integer n, the factorial of n is the product of all positive integers less than or equal to n. It is denoted as n! and defined as n! = 1 x 2 x 3 x … x n. For example: 8! = 1 x 2 x 3 x … x 7 x 8 = 40,320.

Note: 0! is defined as 1.

Additionally, we can define the factorial recursively as:

  1. 0! = 1
  2. (n+1)! = n! x (n+1) with n>0

The table below shows the factorial of several positive integers.

nn!nn!
01151,307,674,368,000
11202,432,902,008,176,640,000
222515,511,210,043 x 10^25
365030,414,093,202 x 10^64
4247011,978,571,670 x 10^100
512010093,326,215,444 x 10^157
672017112,410,180,702 x 10^309
75,04045017,333,687,331 x 10^1000
840,320100040,238,726,008 x 10^2567
9362,880324964,123,376,883 x 10^10000
103,628,8001000028,462,596,809 x 10^35659

The factorial of positive integers grows very quickly. Therefore, an appropriate data type should be used in Java to store these values.

2. The factorial program in Java uses the loop

The following code uses a for loop to calculate the factorial of a positive integer in Java.

package factorial;
import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        int num;
        System.out.print("Enter the number to calculate the factorial: ");
        try (Scanner scanner = new Scanner(System.in)) {
            num = scanner.nextInt();
        }
        long fact = 1;
        if (num != 0 || num != 1) {
            for (int i = 2; i <= num; i++) {
                fact = fact * i;
            }
        }
        System.out.println("Factorial of "+ num + " is " + fact);
    }
}

The for loop can be replaced with a while loop as follows:

package factorial;
import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        int num;
        System.out.print("Enter the number to calculate the factorial: ");
        try (Scanner scanner = new Scanner(System.in)) {
            num = scanner.nextInt();
        }
        long fact = 1;
        if (num != 0 && num != 1) {
            int i = 2;
            while(i<=num) {
                fact = fact * i;
                i++;
            }
        }
        System.out.println("Factorial of "+ num + " is " + fact);
    }
}

We are using the data type long to store the calculated factorial value. If we need to calculate the factorial of larger integers, we need to use BigInteger to store the computed values. Because the data type long cannot store values larger than a certain limit. Refer to part 4 for how to use BigInteger to calculate factorial.

3. The factorial program in Java uses recursion

We can define the factorial recursively as:

  1. 0! = 1
  2. (n+1)! = n! x (n+1) with n>0

The following code uses recursion to calculate the factorial of a positive integer in Java.

package factorial;
import java.util.Scanner;

public class Factorial {
    public static long factorialUsingRecursion(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorialUsingRecursion(n - 1);
}
    public static void main(String[] args) {
        int num;
        System.out.print("Enter the number to calculate the factorial: ");
        try (Scanner scanner = new Scanner(System.in)) {
            num = scanner.nextInt();
        }
        long fact = Factorial.factorialUsingRecursion(num);
        System.out.println("Factorial of "+ num + " is " + fact);
    }
}

4. Calculating factorial for numbers larger than 20 in Java

The data type long can only be used to store factorials of positive integers n<=20. To store factorials of larger integers, we can use BigInteger from the java.math package. BigInteger can store values up to 2^Integer.MAX_VALUE.

package factorial;
import java.math.BigInteger;
import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        int num;
        System.out.print("Enter the number to calculate the factorial: ");
        try (Scanner scanner = new Scanner(System.in)) {
            num = scanner.nextInt();
        }
        BigInteger fact = BigInteger.ONE;
        if (num != 0 || num != 1) {
            for (int i = 2; i <= num; i++) {
                fact = fact.multiply(BigInteger.valueOf(i));
            }
        }
        System.out.println("Factorial of "+ num + " is " + fact);
    }
}

This article has explained calculating factorials using loops and recursion in Java. It is essential to use the appropriate data type, either long or BigInteger, to store the calculated factorials depending on the input value of n.

5/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *