All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Fibonacci Series Using Recursion In Java

Last Updated : Mar 11, 2024

Fibonacci Series Using Recursion In Java

In this article we will show you the solution of fibonacci series using recursion in java, an Fibonacci Series is indeed a set of numbers where each number (aside from the first two) equals the sum of the two before it.

Typically, a Fibonacci series begins with 0 and 1. Iteration or recursion can be used in Java to generate the Fibonacci sequence. This article will discuss a Fibonacci series using Java recursion.

In order to conduct recursion when computing the Fibonacci Series in Java, a function must first be created.

The input for this function is an integer. The function determines if the input number was 0, 1, or 2, if it is, then returns 0, 1, and 1 (for the second Fibonacci), correspondingly.

When the input value is more than 2, this function runs itself repeatedly for the prior values until the input value is equal to or less than 2.

The recursive method takes an exponentially long O(2n) amount of time to solve the Fibonacci series.

If we take into account the recursive stack, then case complexity of a recursive technique is O(n).

Complexity with exponential time is very inefficient. The recursive technique would take too long for compute the Fibonacci series with a very long length.

The Fibonacci Series can be made using memorization technique to address this issue.

The recursive method is more slower than this one. The Fibonacci series is a series where the first and second elements are 0 and 1, respectively, and where the remainder of the nth term equals the sum of the n-1st and n-2nd terms.

Step By Step Guide On Fibonacci Series Using Recursion In Java :-

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
 Scanner in = new Scanner(;
 int n,a=0,b=1,c=0;
    System.out.println("Enter the value of n");
 n = in.nextInt();
    System.out.print(a+" "+b+" ");
 for(int i=3; i<=n; i++){
   c = a+b;
   a = b;
   b = c;
      System.out.print(c+" ");
  1. First we create an import function with the name java.util.Scanner.
  2. Then we create a class with the name of Main.
  3. Then we create a public static void main as string args.
  4. Then we define the scanner
  5. After that we create an integer number as a0, b2, c0.
  6. After that we define value using system.out.println.
  7. Then output of the 1 and 2 elements is then displayed.
  8. The result Always update c firstly, second, and last b is then displayed.
  9. After that we close program using system.out.print.

Conclusion :-

Recursion programmes perform better when memory is utilised, a programming approach.

This method allows for the reuse of previously calculated results that have been stored (cached).

Each Fibonacci number was calculated separately in the prior method.

However, in this method, we'll use the earlier findings to determine the current Fibonacci value.

As a result, once the Fibonacci of the a value has been calculated, it can be reused without further calculation.

I hope this article on fibonacci series using recursion in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪