In this java program, you will learn how to display the Fibonacci series using recursion in java.

What is the Fibonacci Series?

In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F0=0 and F1=1

And

Fn=Fn-1 + Fn-2

Examples of Fibonacci Series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on.

Fibonacci Series using recursion

Recursion is the basic programming technique in which a function calls itself directly or indirectly. The corresponding function is known as a recursive function.

Java Program to Display Fibonacci Series using Recursion

Java Program to Display Fibonacci Series using Recursion

// Java Program to Display Fibonacci Series using Recursion
import java.util.Scanner;
public class JavaPrograms {
public static int fibonacciRecursion(int n) {
if (n == 0) {
return 0;
}
if (n == 1 || n == 2) {
return 1;
}
return fibonacciRecursion(n - 2) + fibonacciRecursion(n - 1);
}
public static void main(String[] args) {
int number;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a Number: ");
number = scanner.nextInt();
scanner.close();
System.out.print("First " + number + " terms: ");
for (int i = 0; i < number; i++) {
System.out.print(fibonacciRecursion(i) + " ");
}
}
}

## Comments