Java Program to Print Fibonacci Series by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Factorial of a Number by Using Recursion

In this program we are going to see how to print Fibonacci series using recursion by Java programming language.

Java Program to Print Fibonacci Series by Using Recursion

Now let’s see different ways to print Fibonacci series by Using Recursion.

Method-1: Java Program to Print Fibonacci Series By Using Static Input and Recursion

Approach:

  • Declare and initiate four static integer variables say count, first, end and fibo.
  • Declare and initiate an integer variable n with a value which indicates the destination point.
  • Call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method write the logic by using an If statement.
  • Call the same method inside that user defined method to print the Fibonacci number till target.

Program:

class Main
{
    //Declare and initiate four static integer variables say count, first, end and fibo. 
    static int count=0,first=0,end=1,fibo=0;
    public static void main(String[] args)
    {
        //Declare and initiate an integer value n with a value which indicate the destination point.
        int n=20;
        System.out.print("Fibonacci series till the point "+n+" is= "+0+" "+1);
        //call the user defined method
        fibonacciSeries(n);
    }
    
    //define the method
    public static void fibonacciSeries(int n)
    {
        //Increase the value of count by 1
        count++;
        //calculate the value of fibo
        fibo=first+end;
        //Check the condition whether the value of count is continuing till the value reaches.
        if(fibo<=n)
        {
            //print the result
            System.out.print(" "+fibo+" ");
            first=end;
            end=fibo;
            //call the same function recursively
            fibonacciSeries(n);
        }
    }
} 

Output:

Fibonacci series till the point 20 is= 0 1 1 2 3 5 8 13

Method-2: Java Program to Print Fibonacci Series By Using User Input and Recursion

Approach:

  • Declare and initiate four static integer variables say count, first, end and fibo.
  • Declare and initiate an integer variable n and prompt the user for input of a value which indicates the destination point.
  • Call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method write the logic by using an If statement.
  • Call the same method inside that user defined method to print the Fibonacci number till target.

Program:

import java.util.Scanner;
class Main
{
    //Declare and initiate four static integer variable say count, first, end and fibo. 
    static int count=0,first=0,end=1,fibo=0;
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number:");
        //Declare and initiate an integer value n and prompt the user to enter the value.
        int n=sc.nextInt();
        System.out.print("Fibonacci series till the point "+n+" is= "+0+" "+1);
        //call the user defined method
        fibonacciSeries(n);
    }
    
    //define the method
    public static void fibonacciSeries(int n)
    {
        //Increase the value of count by 1
        count++;
        //calculate the value of fibo
        fibo=first+end;
        //Check the condition whether the value of count is continuing till the value reaches.
        if(fibo<=n)
        {
            //print the result
            System.out.print(" "+fibo+" ");
            first=end;
            end=fibo;
            //call the same function recursively
            fibonacciSeries(n);
        }
    }
} 
Output:

Enter a number:100
Fibonacci series till the point 100 is= 0 1 1 2 3 5 8 13 21 34 55 89

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.

Related Java Programs: