Fibonacci Series Using Recursion in Java

Previously we developed the Fibonacci series program in java using iteration (for loop, while loop). Now in this post, we will develop the Fibonacci series program using the recursion technique in the Java programming language.

In the Fibonacci series, the next element is the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it.

Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. As a rule, the expression is Xn= Xn-1+ Xn-2

It is named after an Italian mathematician, Leonardo Fibonacci, who lived in the early thirteenth century. Fibonacci numbers are a series in which each number is the sum of the previous two numbers.

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

The base case for finding factorial
fibonacci(0) = 0
fibonacci(1) = 1

General case for finding factorial
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Algorithm:- fibonacci(n)
if (n<=1) 
then return n
else 
return fibonacci(n-1) + fibonacci(n-2)

Fibonacci Recursive Java Method

Fibonacci recursive method using if-else statement,

public static int fibonacci(int n) {
	if(n<=1) return n; // base case
	else // general case
	return (fibonacci(n-1) + fibonacci(n-2) );
}

Fibonacci recursive method using ternary operator,

Using a ternary operator the logic of the Fibonacci recursive method can be written within a single line.

public static int fibonacci(int n) {
	return (n<=1) ? n : 
	   (fibonacci(n-1) + fibonacci(n-2) );
}

Finding Nth Fibonacci term

import java.util.Scanner;

public class FibonacciSeries {

   public static int fibonacci(int n) {
      if(n<=1) return n; // base case
      else // general case
      return (fibonacci(n-1) + fibonacci(n-2) );
   }

   public static void main(String[] args) {

      int n; // range value

      // Read range value
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter n value: ");
      n = scan.nextInt();

      // find nth fibonacci term
      System.out.println(n+"th Fibonacci term "
		+ " is = "+ fibonacci(n));

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter n value: 7
7th Fibonacci term is = 13

Enter n value: 10
10th Fibonacci term is = 55

Fibonacci Series using Recursion in Java

import java.util.Scanner;
public class FibonacciSeries {
   public static int fibonacci(int n) {
      return (n<=1) ? n : 
	  (fibonacci(n-1) + fibonacci(n-2) );
   }

   public static void main(String[] args) {
      int term; // range value

      // Read range value
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter the term: ");
      term = scan.nextInt();

      // display fibonacci series
      System.out.println("First "+term+" terms of "
		+ "Fibonacci series are: ");

      for(int i=1; i<term; i++) 
         System.out.print(fibonacci(i)+"\t");

       // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter the term: 7
First 7 terms of Fibonacci series are:
1 1 2 3 5 8

Enter the term: 15
First 15 terms of Fibonacci series are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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