Java Program to print fibonacci series up to n numbers using recursion


In this program, You will learn how to print fibonacci series up to n numbers using recursion in java.


Some list are : 0 1 1 2 3 5 8

Example: How to print fibonacci series up to n numbers using recursion in java.

import java.util.Scanner;

class Main {
    int a = 0, b = 1, c;

    Main() {
        System.out.println("\n"+a);
        System.out.println(b);
    }

    void printFibonacci(int nth) {
        if (nth > 2) {
            c = a + b;
            System.out.println(c);
            a = b;
            b = c;

            printFibonacci(nth - 1);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number:");
        int nth = sc.nextInt();

        Main obj = new Main();
        obj.printFibonacci(nth);

    }
}

Output:

Enter a number:10

0
1
1
2
3
5
8
13
21
34