Apply Now Apply Now Apply Now
header_logo
Post thumbnail
JAVA

Fibonacci Series in Java

By Abhishek Pati

The Fibonacci Series is one of the most popular number patterns used in programming and mathematics. Many beginners learn it when starting with Java because it helps them understand loops, logic, and number operations in a simple way.

In Java, the Fibonacci Series is often used as a basic coding example for practice and problem-solving. It is a common topic in coding interviews, assignments, and beginner-level programs, making it an important concept for every Java learner.

Table of contents


  1. TL;DR Summary
  2. Fibonacci Series: Definition
    • Example:
    • How it works:
  3. Different Approaches to Solve the Fibonacci Series in Java
    • A. Iteration (Loop)
    • B. Recursion
    • C. Memoization
  4. Conclusion
  5. FAQs
    • Why do beginners struggle with the Fibonacci Series at first?
    • Which Fibonacci approach is easier to understand for beginners?
    • Why is recursion slower in the Fibonacci Series?
    • When should memoisation be used in Fibonacci programs?
    • Why do companies ask Fibonacci questions in coding interviews?
    • What is the biggest mistake while writing a Fibonacci program in Java?

TL;DR Summary

  • This blog helps you understand the Fibonacci Series in Java with simple examples and easy explanations for every approach.
  • You will learn different ways to solve the Fibonacci Series, including Iteration, Recursion, and Memoization, step by step.
  • By going through the code and explanations, beginners can improve their Java programming logic and problem-solving skills with greater confidence.

💡 Did You Know?

The Fibonacci Sequence was introduced by the Italian mathematician Leonardo of Pisa in the year 1202.

Fibonacci Series: Definition

The Fibonacci Series is a number pattern in which each new number is the sum of the previous two. It usually starts with 0 and 1, and continues like 0, 1, 1, 2, 3, 5, 8, and so on.

Example:

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8…

How it works:

Here, 0 + 1 = 1

Then, 1 + 1 = 2

After that, 1 + 2 = 3

In the same way, the series keeps continuing.

Also Read: Array Data Structures and Algorithms in Java

Want to level up your Java skills beyond basic programs? Join HCL GUVI’s Java Programming course and learn everything from core Java concepts to building real-world scalable applications, cracking interviews, and coding like a pro with industry-ready skills.

Different Approaches to Solve the Fibonacci Series in Java

Here are different approaches for solving the Fibonacci Series in Java, each using a different logic and method of implementation:

A. Iteration (Loop)

The loop approach builds the Fibonacci series step by step using a for loop. It begins with 0 and 1, then keeps adding the last two values to form the next number. After each step, values are updated and shifted forward, and the process continues until the required count is reached.

Code

public class FibonacciIteration {

    public static void main(String[] args) {

        int n = 10;

        int first = 0, second = 1;

        for (int i = 0; i < n; i++) {

            System.out.print(first + ” “);

            int next = first + second;

            first = second;

            second = next;

        }

    }

}

Input: n = 10

Output: 0 1 1 2 3 5 8 13 21 34

Code Explanation:

  • In this code, we set n = 10, and start with first = 0 and second = 1. A for loop runs from 0 to n-1, and in each step, it prints first.
  • Then it calculates the next value using next = first + second, and updates the variables by moving second → first and next → second.
  • This keeps repeating, and the Fibonacci series is printed in order, step by step.
MDN

B. Recursion

The recursive approach solves the problem by a function calling itself. It splits the task into smaller parts, finds the previous two numbers first, and then adds them to get the result. This continues until it reaches 0 or 1, and then the final sequence is formed on the return.

Code

public class FibonacciRecursion {

    static int fibonacci(int n) {

        if (n <= 1) return n;

        return fibonacci(n – 1) + fibonacci(n – 2);

    }

    public static void main(String[] args) {

        int n = 10;

        for (int i = 0; i < n; i++) {

            System.out.print(fibonacci(i) + ” “);

        }

    }

}

Input: n = 7

Output: 0 1 1 2 3 5 8

Code Explanation:

  • Here, a function calls itself to find Fibonacci numbers.
  • If n is 0 or 1, it returns that value directly. Otherwise, it calls itself using fibonacci(n-1) and fibonacci(n-2), adds both results, and returns the answer.
  • In the main loop, each value from 0 to n-1 is passed into the function, and the result is printed to form the series.

C. Memoization

The array-based approach stores values and builds the Fibonacci series one step at a time. It starts with 0 and 1, then each new number is calculated using the previous two stored values. Since results are saved, it avoids repeated work and quickly generates the full sequence.

Code

public class FibonacciMemoization {

    public static void main(String[] args) {

        int n = 10;

        int[] fib = new int[n];

        fib[0] = 0;

        fib[1] = 1;

        for (int i = 2; i < n; i++) {

            fib[i] = fib[i – 1] + fib[i – 2];

        }

        for (int i = 0; i < n; i++) {

            System.out.print(fib[i] + ” “);

        }

    }

}

Input: n = 8

Output: 0 1 1 2 3 5 8 13

Code Explanation:

  • This code uses an array fib[] to store values.
  • First, fib[0] = 0 and fib[1] = 1 are set. Then a loop runs from index 2 to n-1, where each value is calculated using fib[i] = fib[i-1] + fib[i-2].
  • After filling the array, another loop prints all values.

If you want to explore more algorithms like the Fibonacci Series and improve your coding skills, check out HCL GUVI’s DSA using Java course. Learn important DSA concepts with hands-on practice and become more confident in coding and interviews.

Conclusion

The Fibonacci Series is more than just a beginner coding problem. It is one of those small concepts that quietly improve the way you understand patterns, logic, and coding flow in Java. Once you get comfortable with problems like this, coding starts feeling less confusing and more like connecting ideas step by step.

FAQs

Why do beginners struggle with the Fibonacci Series at first?

Most beginners get confused about how the previous two numbers keep changing after every step.

Which Fibonacci approach is easier to understand for beginners?

The loop-based approach feels simpler because the flow is easier to follow step by step.

Why is recursion slower in the Fibonacci Series?

Recursion repeatedly performs the same calculations, increasing execution time.

When should memoisation be used in Fibonacci programs?

Memoisation is useful when you want faster performance for larger Fibonacci numbers.

Why do companies ask Fibonacci questions in coding interviews?

It helps interviewers assess how well you understand logic, loops, recursion, and problem-solving.

MDN

What is the biggest mistake while writing a Fibonacci program in Java?

Many learners forget to update the previous numbers correctly inside the logic.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. TL;DR Summary
  2. Fibonacci Series: Definition
    • Example:
    • How it works:
  3. Different Approaches to Solve the Fibonacci Series in Java
    • A. Iteration (Loop)
    • B. Recursion
    • C. Memoization
  4. Conclusion
  5. FAQs
    • Why do beginners struggle with the Fibonacci Series at first?
    • Which Fibonacci approach is easier to understand for beginners?
    • Why is recursion slower in the Fibonacci Series?
    • When should memoisation be used in Fibonacci programs?
    • Why do companies ask Fibonacci questions in coding interviews?
    • What is the biggest mistake while writing a Fibonacci program in Java?