In mathematics, the factorial of a number (n!) is the product of all positive integers less than or equal to n. In Java, factorial programs are commonly used to teach loops, recursion, and data types.
Examples:
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 0! = 1
Understanding the java program for factorial of a number is essential for beginners and is frequently asked in interviews and exams.
Formulae for Factorial
The mathematical formula is:
n! = n × (n − 1) × (n − 2) × … × 1
Special cases:
- 0! = 1
- 1! = 1
Methods to Find the Factorial of a Number in Java
There are multiple ways to write a java program for factorial, depending on the logic you want to practice.
1. Iterative Solution for Factorial in Java
This is the most commonly used approach and best for beginners.
import java.util.Scanner;
class FactorialIterative {
public static void main(String[] args) {
int num = 5;
long fact = 1;
for (int i = 1; i <= num; i++) {
fact = fact * i;
}
System.out.println(“Factorial of ” + num + ” is: ” + fact);
}
}
This approach clearly demonstrates how loops work in Java.
The complexity of the above method:
- Time Complexity: O(n)
- Space Complexity: O(1)
2. Java Program for Factorial of a Number using Recursion Method
Recursion is another popular way to write a java program for factorial using recursion.
class FactorialRecursion {
static int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
public static void main(String[] args) {
int num = 5;
System.out.println(“Factorial of ” + num + ” is: ” + factorial(num));
}
}
This method breaks the problem into smaller subproblems.
The complexity of the above method:
- Time Complexity: O(n)
- Space Complexity: O(n) (due to recursive stack)
3. Java Program for Factorial Using While Loop
A java program for factorial using while loop is another iterative approach.
class FactorialWhile {
public static void main(String[] args) {
int num = 5;
long fact = 1;
while (num > 0) {
fact = fact * num;
num–;
}
System.out.println(“Factorial is: ” + fact);
}
}
This method is useful when practicing control-flow statements.
4. Java Program for Factorial Using If Else
Sometimes input validation is needed, making java program for factorial using if else useful.
class FactorialIfElse {
public static void main(String[] args) {
int num = 5;
long fact = 1;
if (num < 0) {
System.out.println(“Factorial not defined for negative numbers”);
} else {
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println(“Factorial is: ” + fact);
}
}
}
5. One-line Solution (Using the Ternary Operator)
This concise approach is more advanced and less readable for beginners.
class FactorialTernary {
static int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n – 1);
}
public static void main(String[] args) {
System.out.println(factorial(5));
}
}
The complexity of the above method:
- Time Complexity: O(n)
- Space Complexity: O(n)
6. Factorial Using BigInteger
For large numbers, standard data types overflow. Java’s BigInteger solves this.
import java.math.BigInteger;
class FactorialBigInteger {
public static void main(String[] args) {
int num = 50;
BigInteger fact = BigInteger.ONE;
for (int i = 1; i <= num; i++) {
fact = fact.multiply(BigInteger.valueOf(i));
}
System.out.println(“Factorial of ” + num + ” is: ” + fact);
}
}
This approach is ideal for large factorial calculations.
Why Learn the Java Program for Factorial?
- Strengthens understanding of loops and recursion
- Common interview and exam question
- Helps grasp algorithm complexity
- Builds a foundation for advanced math-based programs
FAQs – Java Program for Factorial
1. What is the best method to calculate factorial in Java?
The iterative approach is best for beginners due to simplicity and low memory usage.
2. Which method is faster: recursion or iteration?
Iteration is generally faster and more memory-efficient than recursion.
3. Can Java calculate factorials of very large numbers?
Yes, using BigInteger, Java can handle extremely large factorial values.
4. Why is the factorial of 0 equal to 1?
By mathematical definition, 0! is set to 1 to make formulas and permutations consistent.
Conclusion
The java program for factorial of a number is a fundamental concept that helps beginners master loops, recursion, and problem-solving logic. Whether you use iterative methods, recursion, or BigInteger for large values, understanding these approaches builds a strong Java foundation and prepares you for real-world coding challenges.
Also Read:
