Get factorial of any number in Java Program
In this tutorial, we will learn how to get the factorial of any number in Java.
Let’s first learn how to determine the factorial value of any number. Suppose we have to find the factorial value of 5.
The Formula is:
n! = n(n-1)!
n! = n(n-1)(n-2)(n-3)…… x 1! [whereas, factorial of 1 or 0 is always 1]
5! = 5x4x3x2x1 = 120
By doing this we are basically just multiplying all the positive integers between 1 and the given integer.
We cannot find the factorial of any negative number because it will give an undefined value as a result.
Explanation:
The standard formula for solving factorial is : n(n-1)(n-2)(n-3)…x1.
So, while taking n as a positive integer, the scale of numbers used in the formula keeps decreasing. e.g., if n=4 then 4(4-1)(4-2)(4-3) = 4*3*2*1 = 25
But if we take n = -4 then the scale of numbers in the formula keeps increasing towards negative infinity.
Example : -4(-4-1)(-4-2)(-4-3)…….-∞ => -4*(-5)*(-6)*(-7)…..-∞. Hence we cannot find the factorial of any negative number.
This is a simple Java Program that will give you the factorial value of a number as output
import java.util.*; public class CodeSpeedy{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter any number : "); int number = sc.nextInt(); int result=1; if(number==1||number==0) { result=1; } else if(number<0) { System.out.println("Factorial of Negative number is not Defined!!"); } else { int temp=number; while(temp>=1) { result*= temp; temp--; } } if(number>=0) System.out.println("Factorial of "+ number+ "! is = "+result); } }
Output :
Enter any number : 5 Factorial of 5! is = 120 Enter any number : -4 Factorial of Negative number is not Defined!!
The limitation of this program is that it does not work with very large numbers. This is because of the use of int data type.
The range of int data type isn’t enough to handle large numbers and hence data overflow will occur. So, we have to make another program which will be able to work with large numbers.
Java Program to find the factorial of large numbers
import java.math.BigInteger; import java.util.Scanner; public class factorial { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter number: "); int n = scan.nextInt(); String f = factorial1(n); if(f=="0") { System.out.println("Factorial of negative number is undefined."); } else System.out.println("Factorial is " + f); } public static String factorial1(int n) { if (n == 1 || n == 0) { return "1"; } else if (n < 0) { return "0"; } else { BigInteger fact = new BigInteger("1"); for (int i = 1; i <= n; i++) { fact = fact.multiply(new BigInteger(i + "")); } return fact.toString(); } } }
BigInteger
Class is used here to deal with large numbers and it will not result in any data overflow.
The loop in this program is similar to that of a normal factorial program loop. The only difference is the object which we have initialized with the value of 1. The final value will be stored in the variable fact and we will display it by using:
return fact.toString(); By using this BigInteger Class we can find the factorial of large or big numbers in this way.
Output:
Enter number: 29 Factorial is 8841761993739701954543616000000
Feel free to ask for any doubts you have in the comments section.
Leave a Reply