Fibonacci Series in Java - 5 Methods with Code & Output
Learn how to print Fibonacci series in Java with 5 different methods. Includes using loops, recursion, and arrays with complete code and explanation.
Method 1: Using For Loop
The most efficient iterative approach using a for loop.
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int first = 0, second = 1;
System.out.println("First " + n + " Fibonacci numbers:");
for (int i = 1; i <= n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
}First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Explanation
This approach:
1. Initialize first=0 and second=1
2. In each iteration, print 'first'
3. Calculate next = first + second
4. Shift: first = second, second = next
5. Repeat n times
Method 2: Using While Loop
Same logic using a while loop structure.
public class Fibonacci {
public static void main(String[] args) {
int n = 10, first = 0, second = 1, count = 1;
System.out.println("First " + n + " Fibonacci numbers:");
while (count <= n) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
count++;
}
}
}First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Explanation
Uses a while loop with a counter variable instead of a for loop. Same logic, different syntax.
Method 3: Using Recursion
Recursive approach - elegant but less efficient.
public class Fibonacci {
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
int count = 10;
System.out.println("First " + count + " Fibonacci numbers:");
for (int i = 0; i < count; i++) {
System.out.print(fibonacci(i) + " ");
}
}
}First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Explanation
The recursive approach:
- Base case: If n <= 1, return n
- Recursive case: Return fib(n-1) + fib(n-2)
- Warning: Exponential time complexity!
Method 4: Storing in Array
Store all Fibonacci numbers in an array for reuse.
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int[] fib = new int[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("First " + n + " Fibonacci numbers:");
for (int num : fib) {
System.out.print(num + " ");
}
}
}First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Explanation
Dynamic Programming approach:
1. Create array of size n
2. Initialize fib[0]=0 and fib[1]=1
3. Fill array: fib[i] = fib[i-1] + fib[i-2]
4. All numbers stored for later access
Frequently Asked Questions
What is the Fibonacci series?
Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
What is the time complexity of recursive Fibonacci?
The recursive approach has O(2^n) time complexity, making it inefficient for large numbers. The iterative approach is O(n).
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler