Fibonacci Series in Java

Last Updated : 31 Jan 2026

Implementing the Fibonacci series in Java is a classic programming exercise that provides an excellent introduction to recursion, dynamic programming, and mathematical concepts. In this section, we will explore different methods to implement the Fibonacci series in Java, discuss their advantages and disadvantages, and delve into the underlying mathematics.

Fibonacci Series

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.

In other words, in the Fibonacci series, the next number is the sum of the previous two numbers. It usually starts with 0 and 1. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.

Before diving into Java code, let's briefly discuss the mathematical properties of the Fibonacci series.

Each number in the sequence (after the first two) is the sum of the two preceding numbers. Formally, if we denote the n-th Fibonacci number as F(n), then:

There are the following four approaches to find the Fibonacci series:

  • Without Using Recursion
  • Using Recursion
  • Using Dynamic Programming
  • Using Queue

Without Using Recursion

The Fibonacci series can be determined by using an iterative approach. Like the iterative approach, the recursive approach starts from the bottom and builds its way up.

Let's see the Fibonacci series program in Java without using recursion.

Example

Compile and Run

Output:

0 1 1 2 3 5 8 13 21 34

Using Recursion

One of the most straightforward ways to generate Fibonacci numbers in Java is by using recursion. The recursive approach directly follows the mathematical definition of the Fibonacci series.

Let's see the Fibonacci series program in Java using recursion.

Example

Compile and Run

Output:

0 1 1 2 3 5 8 13 21 34

This approach has a significant drawback: it recalculates Fibonacci numbers repeatedly. For example, when calculating fibonacci(5), it recalculates fibonacci(3) and fibonacci(4) multiple times. This redundancy leads to exponential time complexity, making it inefficient for large values of n.

Memoization: Overcoming Recursion's Shortcomings

To overcome the inefficiency of the recursive approach, we can employ memoization. It involves storing the results of expensive function calls and returning the cached result when the same inputs occur again. In the context of Fibonacci, this means storing previously calculated Fibonacci numbers to avoid redundant calculations.

Here's how we can implement memoization in Java:

Example

Compile and Run

Output:

0 1 1 2 3 5 8 13 21 34

By storing intermediate results in the memo map, we eliminate redundant calculations, drastically improving the performance of our Fibonacci function. With memoization, the time complexity reduces to linear, making it much more efficient.

Using Dynamic Programming

To implement the Fibonacci series using dynamic programming in Java, we use a bottom-up approach to store the results of subproblems in an array and reuse them to avoid redundant calculations.

Example

Compile and Run

Output:

0 1 1 2 3 5 8 13 21 34

Explanation

The output represents the first ten Fibonacci numbers. The program initialises an array to store computed values, avoiding redundant calculations. It iterates from index 2 onward, summing the two previous numbers. Finally, the main() method prints Fibonacci numbers from 0 to 9, generating 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Using Queue

Using a queue to generate the Fibonacci series in Java is an interesting approach that employs the First-In-First-Out (FIFO) principle. This implementation uses a queue to maintain a dynamic sliding window for calculating the next Fibonacci number, making it both intuitive and efficient for the task.

Example

Compile and Run

Output:

0 1 1 2 3 5 8 13 21 34 

Conclusion

In this section, we have explored various ways to generate Fibonacci numbers in Java. We discussed the recursive approach, its inefficiencies, and how memoization can overcome them. Additionally, we explored the iterative approach, which provides an efficient alternative.

Understanding and implementing the Fibonacci series not only helps in mastering fundamental programming concepts but also provides insights into the elegance and beauty of mathematical sequences. As you continue your journey in Java programming, remember that the Fibonacci series is just one of many mathematical concepts waiting to be explored and implemented in code.


Next TopicJava Programs