Java Program to Display Fibonacci Series

Updated on September 30, 2024
Display Fibonacci Series header image

Introduction

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. In mathematics and computer science, the Fibonacci series serves as a classical example of introductory problems tailored to teach recursive and iterative programming techniques. It's also used in mathematical studies, algorithms, and even in art and nature discussions for its intriguing properties.

In this article, you will learn how to display the Fibonacci series using Java. You'll explore various methods including iterative and recursive approaches. Practical examples will be provided to help you understand how to implement these methods effectively.

Iterative Method to Generate Fibonacci Series

Display a Series Up to N Numbers

  1. Initialize the first two numbers of the series (0 and 1).

  2. Use a loop to generate the subsequent numbers up to N.

  3. Print each number as it's calculated.

    java
    public class FibonacciSeries {
        public static void printFibonacci(int count) {
            int n1 = 0, n2 = 1, n3;
            System.out.print(n1 + " " + n2); // printing 0 and 1
    
            for (int i = 2; i < count; ++i) {
                n3 = n1 + n2;
                System.out.print(" " + n3);
                n1 = n2;
                n2 = n3;
            }
        }
    
        public static void main(String[] args) {
            int count = 10; // number of elements to be shown in the series
            printFibonacci(count);
        }
    }
    

    This code initializes two integers, n1 and n2, to 0 and 1 respectively, and prints them as the first two numbers of the Fibonacci series. The loop runs from 2 to count, calculating the next Fibonacci number, printing it, and updating n1 and n2.

Recursive Method to Generate Fibonacci Series

Display the nth Fibonacci Number

  1. Write a recursive function that returns the nth Fibonacci number.

  2. Base cases are the first two Fibonacci numbers.

  3. Call this recursive function in the main method to display the Fibonacci sequence up to N.

    java
    public class FibonacciSeries {
        public static int fibonacciRec(int n) {
            if (n <= 1) {
                return n;
            }
            return fibonacciRec(n - 1) + fibonacciRec(n - 2);
        }
    
        public static void main(String[] args) {
            int count = 10;
            for (int i = 0; i < count; i++) {
                System.out.print(fibonacciRec(i) + " ");
            }
        }
    }
    

    In this code, the fibonacciRec method calls itself to compute Fibonacci numbers recursively. The main method loops through a count of 10, computing and printing each Fibonacci number using the recursive method.

Conclusion

In Java, generating a Fibonacci series can be approached using iterative or recursive methods, each with its own advantages and peculiarities. The iterative method is straightforward and generally more efficient for larger sequences, whereas the recursive method provides a clear and concise code which is a good example of recursion, although it might not be efficient for large numbers. By exploring these examples, you adapt to employing both techniques depending on the context and specific needs of your application. Make use of these fundamental concepts to enhance your understanding and implementation of Java programming.