Fibonacci Series Program in Java using Recursion

In java, to print Fibonacci series using recursion of a given number we can use loop concepts such as for loop and while loop. Fibonacci number is a number which is equals to the sum of its previous two numbers until nth term (except the first two numbers) provided by the user.

If user enters nth term as 5, then output should be 0 1 1 2 3. In this article we will learn an example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We have to write a java program to fibonacci series using recursion. Our program should take nth number in the series and print the output from 0 to nth number. This will be done in our program. Let’s see an example,

Example:

Explanation:

Base Case Calls:

fibonacci(0) returns []
fibonacci(1) returns [0]

Recursive Computation:

fibonacci(2) computes as follows: It will receives base condition [0,1] from fibonacci(1) and fibonacci(0) and returns sum of [0,1] to the next fibonacci number 1. The list becomes [0,1] for fibonacci(2).

fibonacci(3) receives [0, 1] from fibonacci(2) and [0] from fibonacci(1). It then adds the last two elements of [0, 1] (which are 1 and 0) to get the next Fibonacci number 1. The list becomes [0, 1, 1].

fibonacci(4) receives [0, 1, 1] from fibonacci(3) and [0, 1] from fibonacci(2). It adds 1 (from 1 + 1) to get the next Fibonacci number 2. The list becomes [0, 1, 1, 2].

fibonacci(5) receives [0, 1, 1, 2] from fibonacci(4) and [0, 1, 1] from fibonacci(3). It adds 2 (from 2 + 1) to get the next Fibonacci number 3. The list becomes [0, 1, 1, 2, 3].

Logic to print Fibonacci series using recursion

  • Create a method to perform recursion. This method takes an integer input n and checks whether the input number ‘n’ is 0 and 1 and returns n (0 and 1, and 0 and 1 are base condition), if the input is any one of this two numbers.
  • If n is greater than 1,the function calls itself recursively calculates the Fibonacci number by adding the result of fibonacci(n-1) and fibonacci(n-2). And in the main method, iterates from 0 to 1 and inside the loop, it will calls the fibonacci() method for each index i and prints the result.

Java program to print Fibonacci series using recursion

Program 1: Using while loop

In the below program we are using while loop to print fibonacci series using recursion.

import java.util.Scanner;
public class FibonacciSeriesRecursion {
      public static int fibonacci(int n) {    	  
    	  if(n==0|| n==1) {
    		  return n; //returns 0 and 1 (fixed values)
    	  }
    	  else {
    		  return fibonacci(n-1)+fibonacci(n-2);
    	  }
      }
	public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter the number of terms for Fibonacci Series:");
      int n = s.nextInt();
      int i=0;
      System.out.println("Fibonacci series are:");
      while(i<n) {
    	  System.out.print(fibonacci(i)+"\t");
    	  i++;
      }
      s.close();
	}
}

Output:

Enter the number of terms for the Fibonacci Series:
6
Fibonacci series are:
0 1 1 2 3 5

Java program explanation to print Fibonacci series using recursion

  • This Java program generates the Fibonacci series using recursion.
  • It defines a method fibonacci() which takes an integer n as input and returns the nth Fibonacci number.
  • If n is 0 or 1, the method returns n, as these are the base cases for the Fibonacci sequence.
  • Otherwise, it recursively calls itself with n-1 and n-2, and returns the sum of the results.
  • In the main() method, it asks the user to enter the last number in the Fibonacci series.
  • It reads the input and stores it in the variable n.
  • Using a while loop, it iterates from 0 to n-1, printing each Fibonacci number by calling the fibonacci() method.
  • Finally, it closes the Scanner object to release system resources.

Program 2: Using for loop

In the below program we are using for loop to print fibonacci series using recursion.

import java.util.Scanner;
public class FibonacciSeriesRecursion {
      public static int fibonacci(int n) {    	  
    	  if(n==0|| n==1) {
    		  return n; //returns 0 and 1 (fixed values)
    	  }
    	  else {
    		  return fibonacci(n-1)+fibonacci(n-2);
    	  }
      }
	public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter the number of terms for Fibonacci Series:");
      int n = s.nextInt();
      System.out.println("Fibonacci series are:");
      for(int i=0;i<n;i++) {
    	  System.out.print(fibonacci(i)+"\t");
      }  
      s.close();
	}
}

Output:

Enter the number of terms for the Fibonacci Series:
9
Fibonacci series are:
0 1 1 2 3 5 8 13 21

Java program explanation to print fibonacci series using recursion

  • This Java program generates the Fibonacci series using recursion.
  • It defines a method fibonacci() which takes an integer n as input and returns the nth Fibonacci number.
  • If n is 0 or 1, the method returns n, as these are the base cases for the Fibonacci sequence.
  • Otherwise, it recursively calls itself with n-1 and n-2, and returns the sum of the results.
  • In the main() method, it asks the user to enter the last number in the Fibonacci series.
  • It reads the input and stores it in the variable n.
  • Using a for loop, it iterates from 0 to n-1, printing each Fibonacci number by calling the fibonacci() method.
  • Finally, it closes the Scanner object to release system resources

Conclusion

The programs we did here gives us fibonacci series using recursion by using for loop and while loop.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.