The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In Java, printing the Fibonacci series is a common program used to understand loops, variables, and basic logic building.
- It helps in learning loop concepts and iteration in Java
- It improves problem-solving and logical thinking skills
The Fibonacci series can be represented using the following formula:
F(n) = F(n−1) + F(n−2), F(0) = 0, F(1) = 1
Example of the Fibonacci Series in Java:
Input: N = 10
Output: 0 1 1 2 3 5 8 13 21 34
Explanation: Here, first term of Fibonacci is 0 and second is 1, so that 3rd term = first(o) + second(1) etc and so on.
Ways to write the Fibonacci Series program in Java
It includes different approaches like using loops, recursion, and dynamic programming to generate the Fibonacci sequence.
1. Fibonacci Series Using the Iterative Approach
Initialize the first and second numbers to 0 and 1. Following this, we print the first and second numbers. Then we send the flow to the iterative while loop where we get the next number by adding the previous two numbers and simultaneously, we swap the first number with the second and the second with the third.
Example:
import java.io.*;
class Geeks
{
// Function to print N Fibonacci Number
static void Fibonacci(int N)
{
int n1 = 0, n2 = 1;
for (int i = 0; i < N; i++) {
// Print the number
System.out.print(n1 + " ");
// Swap
int n3 = n2 + n1;
n1 = n2;
n2 = n3;
}
}
// Driver Code
public static void main(String args[])
{
// Given Number N
int N = 10;
// Function Call
Fibonacci(N);
}
}
Output
0 1 1 2 3 5 8 13 21 34
2. Fibonacci Series Using Recursive Approach
Since the Fibonacci Number is the summation of the two previous numbers. We can use recursion as per the following conditions:
- Get the number whose Fibonacci series needs to be calculated.
- Recursively iterate from value N to 1.
Base case:
- The base case should be:
if (n <= 1) return n; - The explanation should clarify: “Return
nifnis 0 or 1, otherwise returnfib(n-1) + fib(n-2).”
Recursive call: Recursively call fib(i) for i = 0 to N-1 to generate the first N Fibonacci numbers.
recursive_function(N - 1) + recursive_function(N - 2);
Return statement: At each recursive call(except the base case), return the recursive function for the previous two values as:
recursive_function(N - 1) + recursive_function(N - 2);

Example:
//Driver Code Starts
import java.io.*;
class Geeks
{
// Function to print the fibonacci series
//Driver Code Ends
static int fib(int n)
{
// Base Case
if (n <= 1)
return n;
// Recursive call
return fib(n - 1) + fib(n - 2);
}
// Driver Code
public static void main(String args[])
{
// Given Number N
int N = 3;
// Print the first N numbers
for (int i = 0; i < N; i++) {
System.out.print(fib(i) + " ");
}
}
}
Output
0 1 1
3. Fibonacci Series Using Memoization
In the simple recursive approach above, the time complexity is O(2^N). Using memoization, we can reduce it to O(N), because each Fibonacci number is computed only once and stored in an array." This is because the function computes each Fibonacci number only once and stores it in the array.
Example:
import java.io.*;
class Geeks
{
public static void main(String[] args)
{
// Number of terms to print
int n = 10;
int[] memo = new int[n + 1];
for (int i = 1; i <= n; i++) {
System.out.print(fibonacci(i, memo) + " ");
}
}
public static int fibonacci(int n, int[] memo)
{
if (memo[n] != 0)
return memo[n];
if (n == 1 || n == 2)
return 1;
else {
memo[n] = fibonacci(n - 1, memo)
+ fibonacci(n - 2, memo);
return memo[n];
}
}
}
Output
1 1 2 3 5 8 13 21 34 55
Note:
Starts from fibonacci(1) instead of fibonacci(0), so 0 is not included. If you want to include 0, adjust the range to 0 to n-1 and add handling for fibonacci(0) = 0.
4. Fibonacci Series Using Dynamic Programming
We can avoid the repeated work done in method 2 by storing the Fibonacci numbers calculated so far.
The steps to writing the Program are mentioned below:
- Create an array arr[] of size N.
- Initialize arr[0] = 0, arr[1] = 1.
- Iterate over [2, N] and update the array arr[] as: arr[i] = arr[i - 2] + arr[i - 1]
- Print the value of arr[N].
Example:
import java.io.*;
class Geeks
{
static int fib(int n)
{
// Declare an array to store
// Fibonacci numbers.
// 1 extra to handle case, n = 0
int f[] = new int[n + 2];
int i;
// 0th and 1st number of
// the series are 0 and 1
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
// Add the previous 2 numbers
// in the series and store it
f[i] = f[i - 1] + f[i - 2];
}
// Nth Fibonacci Number
return f[n];
}
public static void main(String args[])
{
// Given Number N
int N = 10;
// Print first 10 term
for (int i = 0; i < N; i++)
System.out.print(fib(i) + " ");
}
}