Factorial of a number Program in Java
Factorial of a number in Java :
Factorial of a number is the product of all numbers between 1 and the number itself.
- For 0, the factorial is 1
- For negative numbers, the factorial values are not defined
Examples
5 : Factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120
7 : Factorial of 7 is 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
7 : Factorial of 7 is 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
Methods Discussed
- Method 1: Iterative
- Method 2: Recursive
- Method 3: BigIntegers
- Method 4: One line Ternary Operator
Method 1
- Initialize variable fact = 1
- Run a loop between 1 and fact with iteration in (i)
- For each iteration in (i), multiple it with fact
- fact = fact * i
- Print the result
//Java program to find factorial of a number import java.util.Scanner; public class LearnCoding { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number : "); int num = sc.nextInt(); if(num >= 0) { System.out.println(num + " Factorial: " + getFact(num)); } else System.out.println("Negative Number: No Factorial"); } private static int getFact(int num) { if(num == 1 || num == 0) return 1; return num * getFact(num-1); } }
Output
Enter a number : 5 5 factorial: 120
Method 2 (Recursive)
This method uses recursion in Java. For an input num –
- Call a recursive function getFact(num)
- Set base case as num == 1, return 1
- For other cases return num * getFact(num-1);
Below is the code for the same –
//Java program to find factorial of a number import java.util.Scanner; public class LearnCoding { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number : "); int num = sc.nextInt(); if(num >= 0) { System.out.println(num + " Factorial: " + getFact(num)); } else System.out.println("Negative Number: No Factorial"); } private static int getFact(int num) { if(num == 1 || num == 0) return 1; return num * getFact(num-1); } }
Output
Enter a number : 6 6 Factorial: 720
Method 3 (BigInteger)
Factorial of a number can be very very large. We can use long instead of int but still factorial will become big enough to not to be stored in long integers as well.
Limits for Factorial
Int : Factorial(13) : 6,227,020,800
We wont be able to calculate beyond 13 factorial
Factorial(14): 87,178,291,200 will violate int limit which is 2,147,483,647
Long : Factorial (19): 121,645,100,408,832,000
We wont be able to calculate beyond 19 factorial
Factorial(20): 2,432,902,008,176,640,000 will violate long limit which is 9,223,372,036,854,775,807
We wont be able to calculate beyond 13 factorial
Factorial(14): 87,178,291,200 will violate int limit which is 2,147,483,647
Long : Factorial (19): 121,645,100,408,832,000
We wont be able to calculate beyond 19 factorial
Factorial(20): 2,432,902,008,176,640,000 will violate long limit which is 9,223,372,036,854,775,807
Solution
Use BigIntegers in Java
// Java program to find factorial of a number of large numbers import java.math.BigInteger; import java.util.Scanner; public class LearnCoding2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = 30; BigInteger fact = BigInteger.ONE; for(int i = 1; i <= num;i++) { // we are doing fact = fact * i // big integer multiplication happens like this fact = fact.multiply(BigInteger.valueOf(i)); } System.out.println(num + " Factorial: \n" + fact); } }
Output
30 Factorial: 265252859812191058636308480000000
Method 4 (One line Ternary Operator)
We can use the ternary operator to find factorial in one single line.
Note this also uses recursion in Java
import java.util.Scanner; public class LearnCoding { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number : "); int num = sc.nextInt(); if(num >= 0) { System.out.println(num + " Factorial: " + getFact(num)); } else System.out.println("Negative Number: No Factorial"); } private static int getFact(int num) { // single line to find factorial return (num == 1 || num == 0) ? 1 : num * getFact(num - 1); } }
Output
Enter a number : 5 5 Factorial: 120
code in python
num = int(input(“Enter a number “))
fact=1
if num <= 0:
print("negative factorial")
elif num == 1:
print("1 is the factorial")
else:
for i in range(1,num+1):
fact=fact*i
print(fact)