Finding Factorial of a Number in Java
The factorial of a number is defined is the product of natural numbers from one to that particular number. Mathematically,
n! = 1 * 2 * 3 * .... * (n1) * n
For example, the factorial of 4 is
4! = 1 * 2 * 3 * 4 = 24
This article will explain you how to find the factorial of a number through iteration as well as recursion.
Finding factorial of a number in Java using Iteration
Let the number whose factorial is to be found be stored in the variable n. A new variable 'result' of type int is declared and initialised with the integer 1.
int result = 1;
Now, our task is to multiply the variable result with all natural numbers from 1 to n. For this purpose, we use a for loop with a counter i that ranges from 1 to n. Within the loop, the existing value of result will be multiplied with the loop counter.
for (int i = 1; i <= n; i++) {
result = result * i;
}
Let us take a small number n = 3 to understand how the above loop works. Before entering the loop, result would be initialised to one. The loop will execute thrice with the value of i = 1, 2 and 3. When the value of i becomes 4, the loop condition fails. When the value of i is 1, the existing result would be multiplied with 1 which again gives one. In the second iteration, result will be multiplied with 2 and in the third iteration with 3. These calculations are shown below:
result = 1
i = 1 result = result * i = 1 * 1 = 1
i = 2 result = result * i = 1 * 2 = 2
i = 3 result = result * i = 2 * 3 = 6
When the loop exists, the value result which was initially one would be already multiplied by all natural numbers from 1 to n. Thus, result holds the factorial of the number.
Given below is a program which finds the factorial of the number 7.
public class Factorial {
public static void main(String[] args) {
int n = 7;
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
System.out.println("The factorial of 7 is " + result);
}
}
The output of the above program would be
The factorial of 7 is 5040
We can start the loop counter, i from 2 instead of 1 because in the first iteration the value of result gets multiplied by 1 which again gives one. The modified loop can be written as
for (int i = 1; i <= n; i++) {
result = result * i;
}
We can also start the loop counter from n and decrement it till 2 or 1. In that case, the loop would look like
for (int i = n; i >= 1; i) {
result = result * i;
}
Given below is another program where the number whose factorial is to be calculated is taken as an input from the user and the result is displayed on the screen.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number whose factorial is to be found: ");
int n = scanner.nextInt();
int result = factorial(n);
System.out.println("The factorial of " + n + " is " + result);
}
public static int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
}
Here is a sample execution
Enter the number whose factorial is to be found: 7
The factorial of 7 is 5040
Finding factorial of a number in Java using Recursion
The factorial of a number be found using recursion also. The base case can be taken as the factorial of the number 0 or 1, both of which are 1. The factorial of some number n is that number multiplied by the factorial of (n1). Mathematically,
factorial ( 0 ) = 1
factorial ( n ) = n * factorial ( n  1 )
Given below is a program which calculates the factorial of 7 using recursion.
public class Factorial {
public static void main(String[] args) {
int n = 7;
int result = factorial(n);
System.out.println("The factorial of 7 is " + result);
}
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n  1);
}
}
}
Integer Overflow when finding factorial
When the factorial program is run with certain inputs 13 and above, we get incorrect results  negative numbers or results which do not match with the actual factorial of that number. The reason is that the factorial of numbers greater than or equal to 13 is too large for the int data type. We can use the long data type but still it wouldn't be large enough to hold the factorial of even higher numbers. The solution is to use the BigInteger class which can handle arbitrarily large numbers which is discussed here. (link will be updated soon )
