Introduction
The Fibonacci series is a sequence of numbers that starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. So, the series begins like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
To find the next number in the series, you simply add the previous two numbers together. For example, to get the number 2, you add 1 and 1. To get the number 3, you add 1 and 2. And so on.

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?
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, 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
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.
How to check Fibonacci series in Java?
In Java, you can iteratively if each number, starting from the second index is the sum of the previous two elements or not. Initialize a variable as true for eg. isFib and set it to false if the current number is not the sum of the previous two, then break the loop. IsFib can be used for tracking if the loop ended normally or not.
What is the Fibonacci series formula?
The nth number in the Fibonacci series can also be calculated using the following formula,
Fn = Fn-1 + Fn-2, where F0 = 0 and F1 = 1.
What is the use of Fibonacci series in programming?
Fibonacci series in programming is used for various tasks, like creating algorithms for efficient calculations, generating random numbers, and modeling patterns found in nature and finance.
Conclusion
In this article, we have discussed the Java program to display the Fibonacci series and how it can be implemented using multiple techniques i.e. using Recursion and without using recursion. With Java, you can also explore C++ and Python-based solutions below.
- Access modifiers in java
- fibonacci series in c
- Java Verify
- Java Tokens
- Fibonacci series in C++
- Fibonacci series in Python
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.