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.
- For‑loop implementation
- While‑loop implementation
- Recursive implementation
- Interactive input example
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:
- Initialize
previous = 0andcurrent = 1. - Loop
maxNumbertimes, printingpreviousand updating values.
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:
- Base cases:
n == 0returns 0;n == 1 || n == 2returns 1. - Recursive case:
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). - 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
- Java Hello World: Your First Program
- Understanding Java: JDK, JRE, and JVM Explained
- Java Variables and Literals: A Comprehensive Guide
- Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
- Java Classes and Objects: A Practical Guide
- Java Recursion: Understanding, Examples, and Trade‑Offs
- Convert JSON to XML in Java with Gson & JAXB – Step‑by‑Step Guide
- Java Program to Identify Armstrong Numbers Using For Loop
- Reverse a String in Java Using Recursion – Step‑by‑Step Guide
- Java Palindrome Number Checker: Algorithms Using While and For Loops