Java Program to Print Fibonacci Series Using Recursion

In this post, you will learn How to Print Fibonacci Series Using Recursion in Java Programming language.

Read More: Java Program to Print Fibonacci Series Using Loop

Print Fibonacci Series

Before getting straight into Java Program to Print Fibonacci Series Using Recursion. Let’s see “What is a Fibonacci Series ?”

A Fibonacci sequence is a sequence in which the next term is the sum of the previous two terms.
The series will go like :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , . . . . . . and so on.

Java Program to Print Fibonacci Series Using Recursion

import java.util.*;

public class Main{
    
    //Fibonacci using recursion
    public static int findFibonacci(int n){
        
        // Base case 1
        if(n == 1){
            return 0;
        }
        // Base case 2
        if(n == 2){
            return 1;
        }
        
        return (findFibonacci(n-1) + findFibonacci(n-2));
        
    }
    
    
    // Main 
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of terms: ");
        int n = sc.nextInt();
        
        for(int i=1; i <= n ; i++){
            System.out.print(""+findFibonacci(i)+", ");
        }
        System.out.println(".");
        
    }
    
}

Output

Enter the number of terms: 12
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, .

How Does This Program Work ?

In the above logic, we have leveraged the use of recursion to find the nth Fibonacci number.
To find the nth Fibonacci number we calculated the (n-1)th fibonacci number and (n-2)th fibonacci number.

fibonacci recurion explaination

So as you can see in the above explanation, we need to have 2 base conditions i.e. Fibonacci number at 1st position (0) and Fibonacci number at 2nd position (1), now Fibonacci at any nth term can be calculated using fib(n) = fib(n-1) + fib(n-2) equation.

//Fibonacci using recursion
    public static int findFibonacci(int n){
        
        // Base case 1
        if(n == 1){
            return 0;
        }
        // Base case 2
        if(n == 2){
            return 1;
        }
        
        return (findFibonacci(n-1) + findFibonacci(n-2));
        
    }

Here, we have created a findFibonacci function which calculates the Fibonacci number at the nth position. If n is 1 i.e. 1st position, then we return 0 and if n is 2 i.e. 2nd position, then we return 1, else we return fib(n-1) + fib(n-2).

for(int i=1; i <= n ; i++){
            System.out.print(""+findFibonacci(i)+", ");
        }
        System.out.println(".");

Using for we are calculating and printing the Fibonacci for every ith position form i = 1 to i = n

Conclusion

I hope after going through this post, you understand how to code Java Program to Print Fibonacci Series Using Recursion.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.