The Fibonacci series is a sequence in which each term is obtained by adding the previous two terms. The series generally starts withĀ 0Ā andĀ 1.
- It is useful for understanding loops, recursion, and dynamic programming in Java.
- Different approaches can be used depending on whether simplicity or efficiency is required.
Example:
Input: N = 10
Output: 0 1 1 2 3 5 8 13 21 34
Explanation:Ā The first two terms areĀ 0Ā andĀ 1. Each subsequent term is calculated by adding the previous two terms.
Fibonacci Series Formula
The Fibonacci series follows:
F(n) = F(n - 1) + F(n - 2)
with:
F(0) = 0Ā andĀ F(1) = 1
Ways to Generate Fibonacci Series
The Fibonacci series can be generated using iterative, recursive, and dynamic programming approaches.
1. Fibonacci Series Using Iterative Approach
The iterative approach uses a loop and two variables to store the previous two terms.
Approach:
- Initialize two variables withĀ 0Ā andĀ 1.
- Print the current term.
- Calculate the next term by adding the previous two terms.
- Update the variables.
- Repeat untilĀ NĀ terms are printed.
class FibonacciIterative {
static void printFibonacci(int n)
{
int first = 0;
int second = 1;
for (int i = 0; i < n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
public static void main(String[] args)
{
int n = 10;
printFibonacci(n);
}
}
Output
0 1 1 2 3 5 8 13 21 34
Explanation: first and second store the two previous Fibonacci terms. The next term is calculated using their sum, and the variables are updated after every iteration.
2. Fibonacci Series Using Recursive Approach
Recursion calculates each Fibonacci term by repeatedly calling the same function for the previous two terms.
Base Case: if (n <= 1)
return n;This returns 0 for n = 0 and 1 for n = 1.

class FibonacciRecursive{
static int fib(int n)
{
// Base case
if (n <= 1) {
return n;
}
// Recursive calls
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args)
{
int n = 8;
for (int i = 0; i < n; i++){
System.out.print(fib(i) + " ");
}
}
}
Output
0 1 1 2 3 5 8 13
Explanation:Ā WhenĀ nĀ isĀ 0Ā orĀ 1, the function returnsĀ n. Otherwise, it recursively calculates the previous two terms and adds them.
3. Fibonacci Series Using Memoization
Memoization is a top-down dynamic programming technique. It stores Fibonacci values that have already been calculated and reuses them when needed.
class FibonacciMemoization{
static int fibonacci(int n, int[] memo)
{
// Base cases
if (n <= 1) {
return n;
}
// Return stored value
if (memo[n] != -1){
return memo[n];
}
// Store calculated value
memo[n] = fibonacci(n - 1, memo)
+ fibonacci(n - 2, memo);
return memo[n];
}
public static void main(String[] args)
{
int n = 10;
int[] memo = new int[n];
// Mark values as not calculated
java.util.Arrays.fill(memo, -1);
for (int i = 0; i < n; i++){
System.out.print(fibonacci(i, memo) + " ");
}
}
}
Output
0 1 1 2 3 5 8 13 21 34
Explanation:Ā TheĀ memoĀ array stores already calculated Fibonacci values. If a value is requested again, the stored result is returned instead of calculating it recursively.
Note: Memoization avoids repeated recursive calculations by storing previously computed Fibonacci values.
4. Fibonacci Series Using Dynamic Programming
The bottom-up dynamic programming approach calculates the Fibonacci numbers from the beginning and stores them in an array.
Approach:
- Create an array of sizeĀ N.
- SetĀ fib[0] = 0.
- SetĀ fib[1] = 1Ā whenĀ N > 1.
- Calculate the remaining terms usingĀ fib[i - 1] + fib[i - 2].
- Print the stored values.
class FibonacciDP {
static void printFibonacci(int n)
{
if (n <= 0) {
return;
}
int[] fib = new int[n];
fib[0] = 0;
if (n > 1) {
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] + " ");
}
}
public static void main(String[] args)
{
int n = 10;
printFibonacci(n);
}
}
Output
0 1 1 2 3 5 8 13 21 34
Explanation: The array stores the Fibonacci terms. Each new value is calculated from the two values already stored before it.