Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Generate Fibonacci Sequence in Java: For, While, and Recursive Examples

What Is the Fibonacci Series in Java?

The Fibonacci series is a sequence where each number equals the sum of the two preceding ones. In Java, the series traditionally starts with 0 and 1:

0, 1, 1, 2, 3, 5, 8, 13, 21, …

Beyond its mathematical intrigue, the Fibonacci sequence is frequently used in algorithm analysis—for instance, in estimating the runtime of recursive procedures and in computing the greatest common divisor.

Fibonacci Series Using a For Loop

// Using a for loop
public class FibonacciExample {
    public static void main(String[] args) {
        int maxNumber = 10;          // Number of terms
        int previous = 0;
        int current = 1;
        System.out.print("Fibonacci Series of " + maxNumber + " numbers:");
        for (int i = 1; i <= maxNumber; i++) {
            System.out.print(previous + " ");
            int next = previous + current;
            previous = current;
            current = next;
        }
    }
}

Output:

Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34

Logic:

Fibonacci Series Using a While Loop

// Using a while loop
public class FibonacciWhileExample {
    public static void main(String[] args) {
        int maxNumber = 10;
        int previous = 0;
        int current = 1;
        System.out.print("Fibonacci Series of " + maxNumber + " numbers:");
        int i = 1;
        while (i <= maxNumber) {
            System.out.print(previous + " ");
            int next = previous + current;
            previous = current;
            current = next;
            i++;
        }
    }
}

Output:

Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34

Fibonacci Series Based on User Input

import java.util.Scanner;
public class FibonacciUserInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of terms: ");
        int maxNumber = scanner.nextInt();
        int previous = 0;
        int current = 1;
        System.out.print("Fibonacci Series of " + maxNumber + " numbers:");
        for (int i = 1; i <= maxNumber; i++) {
            System.out.print(previous + " ");
            int next = previous + current;
            previous = current;
            current = next;
        }
        scanner.close();
    }
}

Logic: The same iterative logic applies, but the term count is supplied by the user.

Fibonacci Series Using Recursion

// Recursive implementation
public class FibonacciRecursion {
    public static int fibonacci(int n) {
        if (n == 0) return 0;
        if (n == 1 || n == 2) return 1;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
    public static void main(String[] args) {
        int maxNumber = 10;
        System.out.print("Fibonacci Series of " + maxNumber + " numbers: ");
        for (int i = 0; i < maxNumber; i++) {
            System.out.print(fibonacci(i) + " ");
        }
    }
}

Output:

Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21 34

Recursion Explained:

  1. Base cases: n == 0 returns 0; n == 1 || n == 2 returns 1.
  2. Recursive case: fibonacci(n) = fibonacci(n-1) + fibonacci(n-2).
  3. Each call splits into two sub‑calls, mirroring the classic definition.

While recursion offers a clean mathematical expression, it incurs exponential time for large n. Use memoization or iterative methods for performance‑critical code.

Java

  1. Java Hello World: Your First Program
  2. Understanding Java: JDK, JRE, and JVM Explained
  3. Java Variables and Literals: A Comprehensive Guide
  4. Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
  5. Java Classes and Objects: A Practical Guide
  6. Java Recursion: Understanding, Examples, and Trade‑Offs
  7. Convert JSON to XML in Java with Gson & JAXB – Step‑by‑Step Guide
  8. Java Program to Identify Armstrong Numbers Using For Loop
  9. Reverse a String in Java Using Recursion – Step‑by‑Step Guide
  10. Java Palindrome Number Checker: Algorithms Using While and For Loops