The factorial program in Java using recursion is one of the most important beginner programs in Java. It helps learners understand:
- How recursion works
- How base conditions prevent infinite calls
- How Java functions call themselves
- How to handle user input properly
Because of this, the factorial of a number in Java using recursion is frequently asked in college exams, coding interviews, and Java practical tests.
What Is a Factorial?
The factorial of a number n is the product of all positive integers less than or equal to n.
Mathematical Definition
n! = n × (n − 1) × (n − 2) × … × 1
Examples
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 3! = 3 × 2 × 1 = 6
- 0! = 1 (by definition)
What Is Recursion in Java?
Recursion is a programming technique where a method calls itself to solve a smaller version of the same problem.
A factorial program in Java using recursion has two essential parts:
- Base Case – Stops recursion
- Recursive Case – Calls the function again
Without a base condition, recursion causes a StackOverflowError.
Factorial Program in Java Using Recursion
This is the most basic version of the factorial program in Java using recursion.
Java Code
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 number = 5;
System.out.println(“Factorial of ” + number + ” is: ” + factorial(number));
}
}
Output
Factorial of 5 is: 120
Factorial of a Number in Java Using Recursion – Step-by-Step Explanation
Let’s understand how recursion works internally for factorial(5):
factorial(5)
= 5 × factorial(4)
= 5 × 4 × factorial(3)
= 5 × 4 × 3 × factorial(2)
= 5 × 4 × 3 × 2 × factorial(1)
= 5 × 4 × 3 × 2 × 1
= 120
The function keeps calling itself until n == 0.
Factorial Program in Java Using Recursion User Input
In real-world programs, we usually take input from users. Let’s modify the factorial program in Java using recursion with user input.
Java Code
import java.util.Scanner;
class FactorialUserInput {
static int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n – 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a number: “);
int num = sc.nextInt();
System.out.println(“Factorial of ” + num + ” is: ” + factorial(num));
}
}
Factorial Program in Java Using Recursion and Scanner
The factorial program in Java using recursion and Scanner allows users to enter a number dynamically.
Key Components
- Scanner class → Takes input
- Recursive method → Calculates factorial
- Base condition → Stops recursion
Why Scanner Is Important
Using Scanner makes the program interactive and practical for:
- Assignments
- Lab exams
- Console-based applications
Factorial Code in Java Using Recursion (Clean Version)
Here’s a clean and interview-ready factorial code in Java using recursion:
import java.util.Scanner;
public class FactorialExample {
static int findFactorial(int n) {
if (n <= 1)
return 1;
return n * findFactorial(n – 1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
int number = scanner.nextInt();
System.out.println(“Factorial is: ” + findFactorial(number));
}
}
Factorial Program in Java Using Recursion – Flow Diagram (Conceptual)
- Start
- Read input
- Call factorial function
- Check base condition
- Return result
- Print factorial
- End
Time and Space Complexity
Time Complexity
O(n)
Because the function is called n times.
Space Complexity
O(n)
Due to recursive call stack memory.
Common Mistakes to Avoid
❌ Missing base condition
❌ Using negative numbers without validation
❌ Forgetting to close Scanner
❌ Stack overflow for large values
When Should You Use Recursion for Factorial?
Use recursion when:
- Learning recursion concepts
- Writing academic programs
- Solving interview questions
Avoid recursion for:
- Very large numbers
- Performance-critical systems
Recursive vs Iterative Factorial (Quick Comparison)
| Feature | Recursive | Iterative |
| Code length | Short | Slightly longer |
| Readability | High | Moderate |
| Memory usage | Higher | Lower |
| Stack overflow risk | Yes | No |
FAQs – Factorial Program in Java Using Recursion
1. What is a factorial program in Java using recursion?
It is a Java program where a method calls itself to calculate the factorial of a number until a base condition is met.
2. How does factorial program in Java using recursion and Scanner work?
The Scanner takes user input, and a recursive function computes the factorial using repeated method calls.
3. Why is 0 factorial equal to 1 in Java?
By mathematical definition, 0! = 1, which also serves as the base case in recursion.
4. Is recursion better than loops for factorial programs?
Recursion is easier to understand conceptually, but loops are more memory-efficient and safer for large inputs.
Conclusion
The factorial program in Java using recursion is a foundational program that strengthens your understanding of recursion, base cases, and function calls. By combining recursion with user input and the Scanner class, you gain practical Java skills essential for exams, interviews, and real-world applications.
If you’re learning Java, mastering this program is a must.
Also Read:
