Home Lifestyle Fibonacci Series Using Recursion in Java Language

Fibonacci Series Using Recursion in Java Language

76
0

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.

<img decoding=

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

      // Read 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

Types of Sorting Algorithms:

Additional Reading

READ MORE

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 😊 Keep Learning !

Previous articleFibonacci Series Program in Java Language
Next articleSum of Series Program in Java Language

LEAVE A REPLY

Please enter your comment!
Please enter your name here