Calculate factorial of a Number example shows how to calculate factorial of a number in Java using for loop (non-recursive), using recursion, and calculating factorial for large numbers using the BigInteger class.
What is the factorial of a number?
Definition of factorial from Wikipedia,
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
In mathematics, a product is a result of multiplying.
For example, factorial of 4 (denoted by 4!) is 4 x 3 x 2 x 1 = 24.
How to calculate the factorial of a number in Java?
The factorial of a number can be calculated in two ways, using a for loop (non-recursive) method or using the recursion.
Using for loop
The factorial of a number can be calculated using a for loop as given in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.javacodeexamples.basic; import java.util.Scanner; public class CalculateFactorialExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); int factorial = 1; /* * loop from 2 to the input number and * multiply them to get the factorial */ for(int i = 2; i <= number; i++){ factorial = factorial * i; } System.out.println("Factorial of " + number + " is: " + factorial); } /* * This method reads int value from console */ private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
Output
1 2 3 |
Input a number 5 Factorial of 5 is: 120 |
Using recursion
The factorial of a number can also be calculated using the recursive method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.javacodeexamples.basic; import java.util.Scanner; public class CalculateFactorialExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); //call recursive method to calculate factorial int factorial = calculateFactorial(number); System.out.println("Factorial of " + number + " is: " + factorial); } /* * Recursive method to calculate factorial */ private static int calculateFactorial(int number) { if(number <= 1) return 1; else return calculateFactorial( number - 1 ) * number; } /* * This method reads int value from console */ private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
Output
1 2 3 |
Input a number 4 Factorial of 4 is: 24 |
How to calculate factorial of large numbers?
Let’s try to calculate a factorial of 50 using the above program.
Output
1 2 3 |
Input a number 50 Factorial of 50 is: 0 |
The result is 0. How did that happen? Well, the factorial of 50 has 12 zeros at the end which is more than an int can hold (the int is 32 bit signed value in Java) so converting it to int value gives us zero.
In order to calculate the factorial of a large number, we need to use the BigInteger class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.javacodeexamples.basic; import java.math.BigInteger; import java.util.Scanner; public class CalculateFactorialExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); BigInteger factorial = BigInteger.valueOf(1); /* * loop from 2 to the input number and * multiply them to get the factorial */ for(int i = 2; i <= number; i++){ factorial = factorial.multiply( BigInteger.valueOf(i) ); } System.out.println("Factorial of " + number + " is: " + factorial); } /* * This method reads int value from console */ private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
Output
1 2 3 |
Input a number 50 Factorial of 50 is: 30414093201713378043612608166064768844377641568960512000000000000 |
This example is a part of the Java Basic Examples.
Please let me know your views in the comments section below.