Recursion in java

Recursion in Java is a programming technique where a method calls itself in order to solve a problem. In a recursive function, the problem is solved by breaking it down into smaller instances of the same problem. Each recursive call reduces the original problem into a simpler or smaller one, and the process continues until a base case is reached, at which point the function stops calling itself.

Here’s a simple example of a recursive function in Java to calculate the factorial of a number:

public class FactorialExample {
    public static void main(String[] args) {
        int number = 5;
        long result = factorial(number);
        System.out.println("Factorial of " + number + " is: " + result);
    }

    // Recursive method to calculate factorial
    static long factorial(int n) {
        // Base case: factorial of 0 is 1
        if (n == 0) {
            return 1;
        } else {
            // Recursive case: n! = n * (n-1)!
            return n * factorial(n - 1);
        }
    }
}

In this example, the factorial method calculates the factorial of a given number using recursion. The base case is when the input is 0, in which case the factorial is 1. Otherwise, the method makes a recursive call to calculate the factorial of n-1 and multiplies it by n.

Key points to remember about recursion in Java:

  1. Base Case:
  • Every recursive method should have one or more base cases that define the simplest scenario where the method returns a result without making further recursive calls.
  • Base cases are essential to prevent infinite recursion.
  1. Recursive Case:
  • The recursive case defines how the problem is reduced into a simpler or smaller instance of the same problem.
  • Each recursive call should bring the problem closer to the base case.
  1. Memory Usage:
  • Recursive methods use the call stack to keep track of each recursive call.
  • Excessive recursion may lead to a stack overflow, especially if the base case is not reached.
  1. Efficiency:
  • While recursion is an elegant solution for certain problems, it may not always be the most efficient one.
  • Some problems may be better solved using iterative approaches.

Recursion is a powerful and expressive programming technique, but it requires careful design to ensure that it terminates correctly and efficiently. Understanding the base case and how the problem is reduced in each recursive call is crucial for implementing recursive algorithms.

Recursion is the process of defining something in terms of itself. As it relates to Java programming, recursion is the attribute that allows a method to call itself.

A method that calls itself is said to be recursive.

The classic example of recursion is the computation of the factorial of a number. The factorial of a number N is the product of all the whole numbers between 1 and N

Example of recursion

A simple example of recursion

class Factorial {
// this is a recursive method
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}

The output from this program

Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

Leave a Comment