Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Fibonacci series in Java?
2.1.
Examples of Fibonacci Series in Java
3.
Fibonacci Series in Java without using Recursion
3.1.
Implementation in Java
4.
Fibonacci Series in Java using Recursion
4.1.
Implementation in Java
5.
Fibonacci Series in Java Using Iterative Approach
5.1.
Implementation in Java
6.
Fibonacci Series in Java Using Dynamic Programming
6.1.
Implementation in Java
7.
Frequently Asked Questions
7.1.
What is the Fibonacci series in Java?
7.2.
What is the pattern of 1 1 2 3 5 8 in Java?
7.3.
What is Fibonacci series code? 
7.4.
How to print Fibonacci series in Java using scanner?
7.5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Fibonacci Series in Java

Author Parth Jain
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Understanding the Fibonacci Series in Java: A sequence of numbers where each number is the sum of the two preceding ones, commonly initiated with 0 and 1. It is widely used in programming and algorithms for its recursive properties.

Fibonacci Series in Java

This blog will help you understand the Fibonacci series in Java. In this article, we will see the Java Program to display Fibonacci series. Before coding, it is essential to understand what a Fibonacci Series is and what logic is required to solve the problem.

What is a Fibonacci series in Java?

In Java, a Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting with 0 and 1. It is often implemented using recursion or iteration. 

Examples of Fibonacci Series in Java

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Here, the next number is found by adding up the two numbers before it.
The number 2 is found by adding the two numbers before it hence (1+1),
Similarly, 3 is found by adding the two numbers before it (1+2),
And 5 is (2+3),
For example, the following number in the sequence above is 21+34 = 55

There are different ways to write the Fibonacci series program in Java:

  • Finding the Fibonacci series in Java without recursion
  • Finding Fibonacci series in Java with recursion
  • Fibonacci Series in Java Using Iterative Approach
  • Fibonacci Series in Java Using Dynamic Programming
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Fibonacci Series in Java without using Recursion

Learn how to generate the Fibonacci series in Java without using recursion.

Implementation in Java

//Fibonacci Series in Java without using recursion


class Fibonacci{  
public static void main(String args[])  
{   
//Considering the first two numbers as 0 and 1 
 int num1=0,num2=1,num3,i,count=10;  
//Count=10 means that only the first 10 fibonacci numbers will be displayed  
 System.out.print(num1+" "+num2);
//printing 0 and 1
 for(i=2;i<count;++i)  
//looping is initiated from 2 as 0 and 1 are already printed  
 {
  num3=num1+num2;
  System.out.print(" "+num3);
  num1=num2;
  num2=num3;
 }
}} 


Analysis of Fibonacci Series in Java without using recursion:

Time Complexity: O(N) 

Auxiliary Space: O(1)

Fibonacci Series in Java using Recursion

Learn how to generate the Fibonacci series in Java using recursion.

Implementation in Java

//Fibonacci Series in Java using recursion

class FibonacciRec{  
 static int num1=0,num2=1,num3=0;    
 static void printFibonacci(int count){    
    if(count>0){    
         num3 = num1 + num2;    
         num1 = num2;    
         num2 = num3;    
         System.out.print(" "+num3);   
         printFibonacci(count-1);    
     }    
 }    
 public static void main(String args[]){    
  int count=10;    
  System.out.print(num1+" "+num2);
//printing 0 and 1    
  printFibonacci(count-2);
//n-2 as 2 numbers are already printed   
 }  
}  

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java using recursion:

Time Complexity: O(2N)  

Auxiliary Space: O(1)

Fibonacci Series in Java Using Iterative Approach

Learn how to generate the Fibonacci Series in Java Using Iterative Approach.

Implementation in Java

//Fibonacci Series in Java Using Iterative Approach

public class FibonacciSeries {
   public static void main(String[] args) {
       int n = 10; // Number of Fibonacci numbers to generate
       long[] fibonacci = new long[n];
       // Initialize the first two Fibonacci numbers
       fibonacci[0] = 0;
       fibonacci[1] = 1;
       // Generate the Fibonacci series iteratively
       for (int i = 2; i < n; i++) {
           fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
       }
       // Print the Fibonacci series
       System.out.println("Fibonacci Series:");
       for (int i = 0; i < n; i++) {
           System.out.print(fibonacci[i] + " ");
       }
   }
}

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java Using Iterative Approach:

Time Complexity: O(N)  

Auxiliary Space: O(N)

Fibonacci Series in Java Using Dynamic Programming

Learn how to generate the Fibonacci Series in Java Using Dynamic Programming.

Implementation in Java

//Fibonacci Series in Java Using Dynamic Programming

public class FibonacciDP {
    public static void main(String[] args) {
        int n = 10; // Change n to the desired number of terms
        long[] fib = new long[n];

        fib[0] = 0; // First Fibonacci number
        fib[1] = 1; // Second Fibonacci number

        // Calculate the Fibonacci series using dynamic programming
        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        // Print the Fibonacci series
        System.out.println("Fibonacci Series:");
        for (int i = 0; i < n; i++) {
            System.out.print(fib[i] + " ");
        }
    }
}
 

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java Using Dynamic Programming:

Time Complexity: O(N)  

Auxiliary Space: O(N)

Frequently Asked Questions

What is the Fibonacci series in Java?

A Fibonacci Sequence is a series of whole numbers such that every third number is equal to the sum of the previous two numbers.

What is the pattern of 1 1 2 3 5 8 in Java?

The pattern "1 1 2 3 5 8" represents the Fibonacci sequence. In this sequence, each number is the sum of the two preceding ones, starting with 1. The pattern continues indefinitely: 13, 21, 34, and so on.

What is Fibonacci series code? 

A Fibonacci series code in Java generates a sequence where each number is the sum of the two preceding ones. It typically starts with 0 and 1. The series can be implemented using recursion or iteration. For example, the code iteratively calculates and prints the Fibonacci sequence based on user-defined length.

How to print Fibonacci series in Java using scanner?

To print the Fibonacci series in Java using Scanner, import the java.util.Scanner class. Use Scanner to get user input for the desired length of the series. Then, employ an iterative loop to calculate and print the Fibonacci sequence. For example, using Scanner to take user input for the series length and printing the resulting Fibonacci sequence.

Conclusion

In conclusion, implementing the Fibonacci series in Java involves using either recursion or iteration. The series starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. The choice between recursion and iteration depends on efficiency and programming preferences. Scanner can be employed to take user input for the length of the series. Overall, understanding the iterative or recursive logic behind the Fibonacci series is crucial for writing effective Java code.

We hope that this blog helped you to enhance your knowledge regarding the Fibonacci series in Java and if you would like to learn more, check out more articles.

Previous article
Java Generics Design Patterns
Next article
Enterprise Java Beans (EJB)
Live masterclass