Java Recursion

Recursion is function calling process that are normally used to solve programming problem. This function execution is based on direct and indirect form. When function calling it self then that is called direct recursion when function call another function that is called indirect recursion. Recursion are very useful technique that are solve complex problem in very effective manner.

Recursion Example

In this section include a simple example which are display a fibonacci series of a given number.

//Fibonacci Series by using recursion
public class FibonacciSeries{


  static void fib(int counter, int initial ,int next ){
    //base condition
    if(counter>0){
      //Display series element
      System.out.print("  "+initial);
      //Call fib function recursively
      fib(counter-1,next,next + initial); 

    }
    
  }
    public static void main(String[] args){
      int counter = 6;
      System.out.print("Fibonacci series of "+counter+" Elements  :"); 
      fib(counter,0,1); //Call fab method

      counter=12;

      System.out.print("\nFibonacci series of "+counter+" Elements :"); 
      fib(counter,0,1); //Call fab method
    }
}
Fibonacci Series Recursion Example

Output

Fibonacci series of 6 Elements  :  0  1  1  2  3  5
Fibonacci series of 12 Elements :  0  1  1  2  3  5  8  13  21  34  55  89

In this example initial variable are displaying the fibonacci number. suppose in case you'll are printing the fibonacci series in reversing order. Then change this recursive algorithm.

///Fibonacci Series in reverse order
// by using recursion
public class FibonacciSeries{


  static void fib(int counter, int initial ,int next ){
    //base condition
    if(counter>0){
      //Call fib function recursively
      fib(counter-1,next,next + initial); 
      //Display series element
      System.out.print("  "+initial);
    }
    
  }
    public static void main(String[] args){
      int counter = 6;
      System.out.print("Fibonacci series of "+counter+" Elements  :"); 
      fib(counter,0,1); //Call fab method

      counter=12;

      System.out.print("\nFibonacci series of "+counter+" Elements :"); 
      fib(counter,0,1); //Call fab method
    }
}
Fibonacci series of 6 Elements  :  5  3  2  1  1  0
Fibonacci series of 12 Elements :  89  55  34  21  13  8  5  3  2  1  1  0

Note that in first example are easily solve using iterative solution. But display in reverse order fibonacci is logical difficult. But recursion is one of the simplest solution to display reverse order fibonacci series.

Recursion Control Mechanism

Recursion is directly and indirectly function calling process. When function are executed, then memory is allocating to their local variable in stack area. And when function scope are ends then this memory are automatically free.

So That is very useful mechanism and generally exist in all modern programming language. But recursion is based on programming logic when programmer logic fail then recursion can executing infinitely. So terminating condition are very useful which is control the execution of recursion. So include a base condition which is stop the function calling process when get desired output.

Base condition

That is condition which is decided the recursion of recursion. At least one base condition are compulsory in recursion. For example.

public class Factorial{

  static int factorial(int number){
    //base condition
    if(number>0){
      return factorial(number-1)*number; 
    }else{
      //when base condition false
      return 1;
    }
  }
    public static void main(String[] args){
      int number = 7;
      System.out.print("Factorial of "+number+" is  :"+ factorial(number)); 
    }
}
Factorial of 7 is  :5040


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved