**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:

- 0! = 1
- (n+1)! = n! x (n+1) with n>0

The table below shows the factorial of several positive integers.

n | n! | n | n! |
---|---|---|---|

0 | 1 | 15 | 1,307,674,368,000 |

1 | 1 | 20 | 2,432,902,008,176,640,000 |

2 | 2 | 25 | 15,511,210,043 x 10^25 |

3 | 6 | 50 | 30,414,093,202 x 10^64 |

4 | 24 | 70 | 11,978,571,670 x 10^100 |

5 | 120 | 100 | 93,326,215,444 x 10^157 |

6 | 720 | 171 | 12,410,180,702 x 10^309 |

7 | 5,040 | 450 | 17,333,687,331 x 10^1000 |

8 | 40,320 | 1000 | 40,238,726,008 x 10^2567 |

9 | 362,880 | 3249 | 64,123,376,883 x 10^10000 |

10 | 3,628,800 | 10000 | 28,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:

- 0! = 1
- (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

package. **java.math****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.