Fibonacci Series Java Program Using Recursion

Introduction

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. The sequence typically begins as 0, 1, 1, 2, 3, 5, 8, .... This guide will show you how to generate the Fibonacci series in Java using recursion.

Problem Statement

Create a Java program that:

  • Generates the Fibonacci series up to a specified number of terms using recursion.

Example 1:

  • Input: 10 (first 10 Fibonacci numbers)
  • Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2:

  • Input: 5 (first 5 Fibonacci numbers)
  • Output: 0, 1, 1, 2, 3

Solution Steps

  1. Create a Recursive Method: Write a method that returns the Fibonacci number for a given position.
  2. Loop to Generate the Series: Use a loop to call the recursive method for the required number of terms.
  3. Output the Series: Print the Fibonacci numbers.

Java Program

import java.util.Scanner;

/**
 * Java Program to Generate Fibonacci Series using Recursion
 * Author: https://www.javaguides.net/
 */
public class FibonacciRecursion {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Get the number of terms from the user
        System.out.print("Enter the number of Fibonacci terms: ");
        int n = scanner.nextInt();

        // Step 2: Generate and print the Fibonacci series
        System.out.println("First " + n + " Fibonacci numbers:");
        for (int i = 0; i < n; i++) {
            System.out.print(fibonacci(i) + (i < n - 1 ? ", " : "\n"));
        }
    }

    // Step 3: Recursive method to calculate Fibonacci number
    public static int fibonacci(int n) {
        if (n <= 1) {
            return n; // Base case: return n if n is 0 or 1
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
        }
    }
}

Explanation

Step 1: Get the Number of Terms from the User

  • The program prompts the user to enter the number of terms in the Fibonacci series they want to generate.

Step 2: Generate and Print the Fibonacci Series

  • A loop runs from 0 to n-1, calling the fibonacci method for each index. The resulting Fibonacci number is printed. A conditional ensures that commas are placed between numbers, but not after the last number.

Step 3: Recursive Method to Calculate Fibonacci Number

  • Base Case: If n is 0 or 1, the method returns n. This is because the first two Fibonacci numbers are 0 and 1, respectively.
  • Recursive Case: For n > 1, the method calls itself with n-1 and n-2 and returns their sum. This follows the Fibonacci rule where each number is the sum of the two preceding ones.

Output Examples

Example 1:

Enter the number of Fibonacci terms: 10
First 10 Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2:

Enter the number of Fibonacci terms: 5
First 5 Fibonacci numbers:
0, 1, 1, 2, 3

Conclusion

This Java program uses recursion to generate the Fibonacci series. The recursive approach is straightforward and aligns closely with the mathematical definition of the Fibonacci sequence. However, it can be less efficient for large values of n due to the repeated calculations of the same Fibonacci numbers. In practice, this method is ideal for small to moderate values of n, or for educational purposes to illustrate the concept of recursion.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare