Java Program to Print Fibonacci Series
You can print the Fibonacci series in Java by using a loop to add the last two numbers starting with
0 and 1, like this: int a=0, b=1; for(int i=0; i. Examples
Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
How to Think About It
To print the Fibonacci series, start with the first two numbers 0 and 1. Then, keep adding the last two numbers to get the next one. Repeat this process until you have printed the desired count of numbers.
Algorithm
1
Get the number of terms to print (n).2
Initialize two variables with 0 and 1 as the first two Fibonacci numbers.3
Use a loop to repeat n times:4
Print the current number.5
Calculate the next number by adding the previous two.6
Update the variables to move forward in the series.Code
java
public class Fibonacci { public static void main(String[] args) { int n = 10; // number of terms int a = 0, b = 1; for (int i = 0; i < n; i++) { System.out.print(a + " "); int c = a + b; a = b; b = c; } } }
Output
0 1 1 2 3 5 8 13 21 34
Dry Run
Let's trace printing 5 Fibonacci numbers through the code
1
Initialize variables
a=0, b=1
2
Iteration 1
Print a=0; c=0+1=1; a=1; b=1
3
Iteration 2
Print a=1; c=1+1=2; a=1; b=2
4
Iteration 3
Print a=1; c=1+2=3; a=2; b=3
5
Iteration 4
Print a=2; c=2+3=5; a=3; b=5
6
Iteration 5
Print a=3; c=3+5=8; a=5; b=8
| Iteration | a (printed) | b | c (next) |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 1 | 1 | 2 |
| 3 | 1 | 2 | 3 |
| 4 | 2 | 3 | 5 |
| 5 | 3 | 5 | 8 |
Why This Works
Step 1: Start with first two numbers
The Fibonacci series always begins with 0 and 1, so we set a=0 and b=1.
Step 2: Print current number
In each loop iteration, we print the current number stored in a.
Step 3: Calculate next number
We find the next Fibonacci number by adding a and b, then update a and b to move forward.
Alternative Approaches
Recursive method
java
public class Fibonacci { public static int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } public static void main(String[] args) { int n = 10; for (int i = 0; i < n; i++) { System.out.print(fib(i) + " "); } } }
This method is simple but inefficient for large n because it recalculates values many times.
Using array to store series
java
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]; } for (int i = 0; i < n; i++) { System.out.print(fib[i] + " "); } } }
This stores all Fibonacci numbers but uses extra memory.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs n times, so the time grows linearly with the number of terms.
Space Complexity
Only a few variables are used, so space is constant regardless of n.
Which Approach is Fastest?
The iterative loop method is fastest and most memory-efficient compared to recursion or array storage.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative loop | O(n) | O(1) | Large n, efficient |
| Recursive method | O(2^n) | O(n) | Small n, simple code |
| Array storage | O(n) | O(n) | When all values needed later |
Use a loop with two variables to efficiently generate Fibonacci numbers without extra memory.
Beginners often forget to update both variables correctly, causing an infinite loop or wrong output.