Fibonacci Series in Java: Explained with Examples

The Fibonacci series in Java, an interesting mathematical sequence, possesses a remarkable property: each number within it is the result of adding the two preceding numbers. This simple yet profound pattern has intrigued mathematicians and programmers alike for centuries. In Java programming, harnessing the power of the Fibonacci series program in Java is not just an exercise in mathematical curiosity; it's a practical problem-solving tool. Java provides different methods to generate Fibonacci numbers; each has its own cons, advantages, and challenges. Also, read about the diverse approaches for creating Fibonacci sequences in Java.

Also, this post will help you understand the basic aspects of working with Fibonacci numbers in Java. You will also learn how to identify whether a given number belongs to the Fibonacci sequence, which is valuable for developers. Let us know more about printing the Fibonacci series in Java's built-in scanner, to gain a holistic understanding of using this concept in your Java applications.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Fibonacci Series in Java Using For Loop

One of the elementary techniques to generate Fibonacci numbers in Java involves using a for loop. Here's a Java code snippet illustrating this method:

public class FibonacciUsingForLoop {

    public static void main(String[] args) {

        int n = 10; // The number of Fibonacci numbers to generate

        long[] fib = new long[n];

        

        fib[0] = 0;

        fib[1] = 1;

        

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

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

        }

        

        System.out.println("Fibonacci Series using For Loop:");

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

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

        }

    }

}

Fibonacci Series in Java using While Loop

Another approach to crafting the Fibonacci series in Java is to employ a while loop. This code snippet demonstrates the method:

public class FibonacciUsingWhileLoop {

    public static void main(String[] args) {

        int n = 10; // The number of Fibonacci numbers to generate

        long[] fib = new long[n];

        

        fib[0] = 0;

        fib[1] = 1;

        

        int i = 2;

        while (i < n) {

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

            i++;

        }

        

        System.out.println("Fibonacci Series using While Loop:");

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

            System.out.print(fib[j] + " ");

        }

    }

}

Fibonacci Series in Java Using Recursion

Recursion presents yet another avenue for generating Fibonacci numbers in Java. Here's a Java program using recursion:

public class FibonacciUsingRecursion {

    static long Fibonacci (int n) {

        if (n <= 1) {

            return n;

        }

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

    }

    public static void main(String[] args) {

        int n = 10; // The number of Fibonacci numbers to generate

        

        System.out.println("Fibonacci Series using Recursion:");

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

            System.out.print(fibonacci(i) + " ");

        }

    }

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Fibonacci Series in Java using Memoization

Memoization emerges as a technique that can enhance the efficiency of recursive Fibonacci Java calculations. It involves storing previously computed values. Here's a Java program that utilizes memoization:

public class FibonacciUsingMemoization {

    static long[] memo;

    static long fibonacci(int n) {

        if (n <= 1) {

            return n;

        }

        if (memo[n] != 0) {

            return memo[n];

        }

        memo[n] = fibonacci(n - 1) + fibonacci(n - 2);

        return memo[n];

    }

    public static void main(String[] args) {

        int n = 10; // The number of Fibonacci numbers to generate

        memo = new long[n + 1];

        

        System.out.println("Fibonacci Series using Memoization:");

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

            System.out.print(fibonacci(i) + " ");

        }

    }

}

Fibonacci Series in Java using Iterative Approach

An iterative approach often stands out as the most efficient means of generating Fibonacci numbers. Here's a Java program employing an iterative approach:

public class FibonacciUsingIterative {

    static long fibonacci(int n) {

        if (n <= 1) {

            return n;

        }

        long a = 0, b = 1, c;

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

            c = a + b;

            a = b;

            b = c;

        }

        return b;

    }

    public static void main(String[] args) {

        int n = 10; // The number of Fibonacci numbers to generate

        

        System.out.println("Fibonacci Series using Iterative Approach:");

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

            System.out.print(fibonacci(i) + " ");

        }

    }

}

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Conclusion

In Java, the Fibonacci series program in Java can be generated using an array of methods, including for loops, while loops, recursion, memoization, and iterative approaches. Each approach has unique advantages and considerations, catering to diverse performance and readability requirements. The choice of method ultimately depends on the specific needs of your Java project.

If you are looking to enhance your skills further, we would highly recommend you to check Simplilearn’s Professional Certificate Program in Full Stack Web Development. This course, in collaboration with IIT Madras, can help you hone the right skills and make you job-ready in no time.

If you have any questions or queries, feel free to post them in the comments section below. Our team will get back to you at the earliest.

FAQs

1. How to check Fibonacci numbers in Java?

To determine whether a number belongs to the Fibonacci series in Java, you can use the following method:

public static boolean isFibonacci(int num) {

    int a = 0, b = 1;

    while (b < num) {

        int c = a + b;

        a = b;

        b = c;

    }

    return b == num;

}

This method checks if the given number adheres to the Fibonacci sequence.

2. How to print the Fibonacci series in Java using a scanner?

Printing a Fibonacci series in Java while utilizing a scanner to input the number of terms can be achieved with the following code:

import java.util.Scanner;

public class FibonacciWithScanner {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of Fibonacci terms: ");

        int n = scanner.nextInt();

        scanner.close();

        

        long a = 0, b = 1;

        

        System.out.println("Fibonacci Series:");

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

            System.out.print(a + " ");

            long c = a + b;

            a = b;

            b = c;

        }

    }

}

This code prompts the user for the number of terms they desire in the Fibonacci series and subsequently prints the series accordingly.

3. What is the pattern of 1 1 2 3 5 8 in Java?

The sequence "1 1 2 3 5 8" mirrors the renowned Fibonacci series in Java. In this sequence, each number is the sum of the two preceding ones, commencing with two initial 1s.

4. How do you identify the Fibonacci series?

To identify a Fibonacci series, you must ascertain whether each number in the sequence results from adding the two preceding numbers, starting with two initial values, typically 0 and 1 or 1 and 1. If this fundamental property holds true for a given series, it qualifies as a Fibonacci series. You can also use the Fibonacci method provided earlier in this article to verify if a specific number is part of the Fibonacci sequence. This method checks whether a number adheres to the Fibonacci sequence by iteratively summing the two preceding numbers until reaching or exceeding the given number. If the final result matches the given number, it is indeed a member of the Fibonacci sequence.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.