Recursion in Java

Recursion Definition – Recursion is a technique in programming language, where the function calls itself directly or indirectly is called Recursion.

In this blog, I have added beginner-level examples of recursion.

Breakdown of Recursive Algorithm

Every recursive algorithm follows the below steps

  • Contains Base Case(Stopping point for the algorithm).
  • Work toward the case.
  • Recursively calling itself until it reaches the base case.

5 Beginner Level Recursion Problems.

If you are a beginner to recursion then the following are the very simple and easy level problems that you must know before jumping into Leetcode or GFG.

See also  Java Interview Questions (Part-2) | Read Now

1] Factorial of Number

Logic

  • Declared a mul variable outside the recursion method otherwise, it will also be changed to the initial value once the recursive method calls itself.
  • Added the main logic of how the factorial of a number is calculated.
  • Added the base condition, when the n value becomes 0 or 1 returning the factorial(mul).
  • For each recursive call of method keep decrementing the value of n.
  • Calling the method recursively.
package Recursion;

public class FactorialNumber {
    public static void main(String[] args) {
        int n=5;
        System.out.println(fact(n));
    }
    public static int mul=1;
    public static int fact(int n){
        mul=mul*(n);
        if(n==0||n==1){
            return mul;
        }
        n--;
        return fact(n);
    }
}

2] Fibonacci Series of Number

package Recursion;

public class FibonacciSeries {
    public static void main(String[] args) {
        System.out.println(0);
        System.out.println(1);
        System.out.println(fibonacciSeries(10));
    }
    public static int start=0;
    public static int prev=1;
    public static  int current=1;
    public static int fibonacciSeries(int n){
        if(n-3==0){
            current=start+prev;
            return current;
        }
        current=start+prev;
        start=prev;
        prev=current;
        n--;
        System.out.println(current);
        return fibonacciSeries(n);
    }
}

3] Palindrome

package Recursion;

public class PalindromeUsingRecursion {
    public static void main(String[] args) {
        String s="abbbba";
        System.out.println(checkPalin(s));
    }
    public static boolean checkPalin(String s){
        if(s.length()==0 || s.length()==1){
            return true;
        }
        if(s.charAt(0)==s.charAt(s.length()-1)){
            return checkPalin(s.substring(1,s.length()-1));
        }
        return false;
    }
}

4] Print N numbers in Increasing Order

package Recursion;

public class PrintnNumbers {
    public static void main(String[] args) {
        int n=10;
        System.out.println(printNumbersIncreasingOrder(n));
    }
    public static int start=0;

    public static int printNumbersIncreasingOrder(int n){
        if(start+1==n){
            start++;
            return start;
        }
        start++;
        System.out.println(start);
        return printNumbersIncreasingOrder(n);
    }
}

5] Print N numbers in Decreasing Order

package Recursion;

public class PrintnNumbers {
    public static void main(String[] args) {
        int n=10;
        System.out.println(printNumbersDecreasingOrder(n));
    }
    public static int printNumbersDecreasingOrder(int n){
        if(n==1){
            return n;
        }
        System.out.println(n);
        n--;
        return printNumbersDecreasingOrder(n);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts