Home Lifestyle Fibonacci Series Using Recursion in Java Language

# Fibonacci Series Using Recursion in Java Language

0
90

Fibonacci series program in java using iteration (for loop, while loop). Now in this post, we will develop the Fibonacci series program using the recursion technique in the Java programming language.

In the Fibonacci series, the next element is the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it.

Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. As a rule, the expression is Xn= Xn-1+ Xn-2

## Fibonacci Series Using Recursion in Java Language

``````import java.util.Scanner;

public class FibonacciSeries {

public static int fibonacci(int n) {
if(n<=1) return n; // base case
else // general case
return (fibonacci(n-1) + fibonacci(n-2) );
}

public static void main(String[] args) {

int n; // range value

Scanner scan = new Scanner(System.in);
System.out.print("Enter n value: ");
n = scan.nextInt();

// find nth fibonacci term
System.out.println(n+"th Fibonacci term "
+ " is = "+ fibonacci(n));

// close Scanner class object
scan.close();
}
}
``````

## OUTPUT

``````

Enter n value: 7
7th Fibonacci term is = 13

``````