# 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.

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 Javauses 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 Javauses 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)
0 Shares