Java Recursion
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Recursion

Jeevadharshan

Java Recursion 

Recursion in Java is a programming technique where a method calls itself to solve a problem. It is especially useful for problems that can be divided into smaller, similar subproblems. 
 
Instead of using loops, recursion repeatedly breaks a task down until it reaches a simple case that can be solved directly.  

How Recursion Works in Java 

Every recursive method has two essential parts:
  • Recursive Case → The method calls itself 
  • Base Case (Halting Condition) → Stops recursion
Without a base case, recursion would continue indefinitely, causing a StackOverflowError.

Example 1: Sum of Numbers from 1 to N (Recursion) 
 
This example calculates the sum of all integers from 1 to a given number using recursion. 
 
public class RecursiveSum { 
 
    static int sumUpTo(int n) { 
        if (n == 1) {          // Base case 
            return 1; 
        } 
        return n + sumUpTo(n - 1); // Recursive case 
    } 
 
    public static void main(String[] args) { 
        int result = sumUpTo(6); 
        System.out.println("Sum from 1 to 6 = " + result); 
    } 
}   

Output  

Sum from 1 to 6 = 21 

How it unfolds 

sumUpTo(6) 
= 6 + sumUpTo(5) 
= 6 + 5 + sumUpTo(4) 
= ... 
= 6 + 5 + 4 + 3 + 2 + 1  

Understanding the Base Case (Halting Condition) 

The base case is the condition that stops recursion. In the previous example, recursion stops when n == 1. 
 
Without it: 
 
sumUpTo(6) → sumUpTo(5) → sumUpTo(4) → ... 
 
This would never end and crash the program. 
 
Example 2: Sum of a Number Range 
 
This program calculates the sum between two numbers using recursion. 
 
public class RangeSum { 
 
    static int sumRange(int start, int end) { 
        if (start == end) {       // Base case 
            return start; 
        } 
        return end + sumRange(start, end - 1); 
    } 
 
    public static void main(String[] args) { 
        int total = sumRange(3, 7); 
        System.out.println("Sum from 3 to 7 = " + total); 
    } 
}  

Output 

Sum from 3 to 7 = 25

Example 3: Recursive Countdown 
 
A classic recursion example that prints numbers in reverse order. 
 
public class RecursiveCountdown { 
 
    static void countdown(int n) { 
        if (n == 0) {   // Base case 
            return; 
        } 
        System.out.print(n + " "); 
        countdown(n - 1); // Recursive call 
    } 
 
    public static void main(String[] args) { 
        countdown(5); 
    } 
}  

Output  

5 4 3 2 1 
 
Example 4: Factorial Using Recursion 
 
The factorial of a number n is the product of all positive integers up to n. 
 
n! = n × (n-1) × (n-2) × … × 1 
 
public class FactorialRecursion { 
 
    static long factorial(int n) {  
        if (n <= 1) {     // Base case 
            return 1; 
        } 
        return n * factorial(n - 1); 
    } 
 
    public static void main(String[] args) { 
        int num = 6; 
        System.out.println("Factorial of " + num + " = " + factorial(num)); 
    } 
}  

Output 

Factorial of 6 = 720

When to Use Recursion in Java

Recursion is ideal for:
  • Mathematical computations (factorial, Fibonacci) 
  • Tree and graph traversal 
  • Divide-and-conquer algorithms 
  • Backtracking problems 
  • File system traversal 

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !