Fibonacci Series in Java

Fibonacci Series in Java: With and Without Recursion


🌱 Understanding the Fibonacci Series in Java — With and Without Recursion

If you’ve ever dabbled in programming or even just heard the word "Fibonacci" in passing, chances are you've stumbled upon one of the most famous number patterns in the world. The Fibonacci series is a sequence where each number is the sum of the two numbers before it. It starts off simple:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
And just like that, it keeps going forever.

In this post, we'll explore two easy ways to generate the Fibonacci series using Java:

  1. Using Recursion

  2. Without using Recursion (i.e., using a loop)

Whether you're a beginner trying to understand the concept or brushing up on Java basics, this guide will walk you through it with simple explanations and real-world language.


🌀 What Is the Fibonacci Series, Really?

Think of the Fibonacci series as nature's favorite sequence. It's found everywhere—from sunflower spirals 🌻 to pine cones and even in architecture. In math and programming, it’s a great way to learn about sequences, loops, and recursion.

The rule is straightforward:

Each number in the series is the sum of the previous two numbers.

So, if you start with 0 and 1, the rest unfolds like this:

  • 0 + 1 = 1

  • 1 + 1 = 2

  • 1 + 2 = 3

  • 2 + 3 = 5

  • 3 + 5 = 8
    ...and so on.


🧠 Solution #1: Using Recursion

Recursion is when a function calls itself. This is a bit like those Russian nesting dolls—each function keeps calling a smaller version of itself until it hits the base case.

Here’s the Java code that prints the Fibonacci series recursively:

package com.javacodepoint.programs;

import java.util.Scanner;

public class FibonacciSeriesWithRecursion {

    static int number1 = 0;
    static int number2 = 1;

    public static void main(String[] args) {

        System.out.println("Enter number up to which Fibonacci series to print:");
        int upto = new Scanner(System.in).nextInt();

        System.out.print(number1 + " " + number2);
        printFibonacci(upto - 2); // Already printed first two numbers
    }

    public static void printFibonacci(int count) {
        if (count > 0) {
            int nextNumber = number1 + number2;
            number1 = number2;
            number2 = nextNumber;

            System.out.print(" " + nextNumber);
            printFibonacci(count - 1);
        }
    }
}

💡 Sample Output:

Enter number up to which Fibonacci series to print:
10  
0 1 1 2 3 5 8 13 21 34

Pros:

  • Clean and elegant logic

  • Great for understanding how recursion works

Cons:

  • Not very efficient for large numbers due to function call overhead

  • Can cause a StackOverflowError if the recursion depth is too high


🔁 Solution #2: Without Using Recursion (Using Loops)

For most real-world use cases—especially if you’re dealing with large numbers—loops are more efficient than recursion.

Here's the iterative (non-recursive) version of the Fibonacci program:

package com.javacodepoint.programs;

import java.util.Scanner;

public class FibonacciSeries {

    public static void main(String[] args) {

        System.out.println("Enter number up to which Fibonacci series to print:");
        int upto = new Scanner(System.in).nextInt();

        int number1 = 0;
        int number2 = 1;

        System.out.print(number1 + " " + number2);

        for (int i = 3; i <= upto; i++) {
            int nextNumber = number1 + number2;
            System.out.print(" " + nextNumber);
            number1 = number2;
            number2 = nextNumber;
        }
    }
}

💡 Sample Output:

Enter number up to which Fibonacci series to print:
10  
0 1 1 2 3 5 8 13 21 34

Pros:

  • More efficient and faster

  • No risk of crashing due to deep recursion

Cons:

  • Slightly more code to write

  • Not as conceptually “cool” as recursion 😄


🤔 So, Which One Should You Use?

  • If you're learning recursion or want to demonstrate it in interviews or exams—go for the recursive method.

  • If you're building an app or doing performance-sensitive coding, stick with the loop-based approach.


🚀 Final Thoughts

The Fibonacci series is more than just a bunch of numbers—it's a beautiful example of how simple rules can create complex patterns. Whether you're using recursion or a loop, generating this series in Java is a fantastic way to level up your problem-solving skills.

Now it’s your turn—open your IDE, run the code, and play around. Try changing the starting numbers or extending the logic. Coding is all about experimenting and learning.

Happy coding! 👨‍💻👩‍💻


Post a Comment

0 Comments