Fibonacci Series in Java

Parth Jain
Last Updated: May 13, 2022

Fibonacci Series in Java-Introduction

This blog will help you understand the Fibonacci series program in Java. The Fibonacci series in Java is a program that when given an integer input N, returns a Fibonacci Series of N numbers. Before coding, it is essential to understand what a Fibonacci Series is and what logic is required to solve the problem.

Fibonacci Series in Java-Understanding the Problem Statement

For a given input integer N, print the Fibonacci Series up to the N term.

Example for Fibonacci series in Java: 
Input: N = 9
Output: 0 1 1 2 3 5 8 13 21 

Here we have considered the first term of Fibonacci as 0 and the second as 1 therefore, the term becomes the sum of the first and second numbers, which is 1.

Example for Fibonacci series in Java: 
Input: N = 14
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233  

What is a Fibonacci series in Java?

A Fibonacci series in Java is a sequence of numbers such that every third number is equal to the sum of the previous two numbers. For Example:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Here 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

Fibonacci Series in Java-Solution

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

  • Finding Fibonacci series in Java without recursion
  • Finding Fibonacci series in Java with recursion

Fibonacci Series in Java without using recursion

//Iterative Fibonacci series program in Java

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 for Fibonacci series in Java program :
Time Complexity: O(N) 
Auxiliary Space: O(1)

Fibonacci Series in Java using recursion

//Recursive Fibonacci series program in Java

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   
 }  
}  
Analysis for Fibonacci series in Java program :
Time Complexity: O(2N)  
Auxiliary Space: O(1)

FAQs

  1. What is a Fibonacci Sequence?
    A Fibonacci Sequence is a series of whole numbers such that every third number is equal to the sum of the previous two numbers.
  2. What is the Fibonacci Series formula?
    Fibonacci numbers are created by setting the first number as 0 and the second as 1, then with the help of a recursive formula. Fn = Fn-1 + Fn-2 we get the rest. 
  3. What is the Fibonacci of 5?
    Fibonacci of 5 can be found by finding the sum of Fibonacci of 3 and 4. Therefore, the Fibonacci of 5 becomes 5 itself. Have a look at the following series 0 1 1 2 3 5.
  4. What approach should be taken to print Fibonacci recursively?
    Base case: If the recursively called value is less than 1, return 1.
    Recursive call: If the base case fails, then recursively call for the last two values using: recursive(N – 1) + recursive(N – 2);
  5. How many ways can be used to find Fibonacci Series in Java?
    To find the Fibonacci series in Java, three ways can be used :
    Iterative
    Recursive 
    Dynamic

Key Takeaways

In this article, we have extensively discussed the Fibonacci series program in java and how it can be implemented using multiple techniques. To explore C++ and Python-based solutions check out the following links.
For C++
For Python
We hope that this blog has helped you enhance your knowledge regarding Java and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!

Was this article helpful ?
0 upvotes