5 ways to print the Fibonacci series in java

In this article, we will learn what is a Fibonacci series and what are the 5 ways to print the Fibonacci series in java

Fibonacci series is a series of elements where we sum up the previous 2 elements to generate the next element.

The first 2 elements of the Fibonacci series are 0 and 1.

Print using for loop

For loop will be used when we know exactly how many times we have to iterate the loop.

public class FibonacciIterationForLoop {

  static void printFibonacciElements(int numberOfElements) {

    int firstElement = 0;
    int secondElement = 1;

    System.out.print(firstElement + " " + secondElement);

    for (int i = 2; i < numberOfElements; i++) {
      int nextElement = firstElement + secondElement;
      System.out.print(" " + nextElement);
      firstElement = secondElement;
      secondElement = nextElement;
    }
  }

  public static void main(String[] args) {
    int numberOfElements = 10;
    printFibonacciElements(numberOfElements);
  }
}

Output : 0 1 1 2 3 5 8 13 21 34 

Explanation

  1. Print the first 2 elements
  2. Start the for loop with initialization and end condition
  3. Add up the 2 elements to generate the next element
  4. Swap the elements (the first element will be swapped with the second element and the second will be swapped with the next element )
  5. Repeat the 2nd and 3rd steps till the end condition is met

Iterations will be like below

0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13
8 + 13 = 21
13 + 21 = 34

Print using a while loop

While loop will be used when we do not know exactly how many times we have to iterate the loop but we are aware of the end condition.

package com.fibo;

public class FibonacciIterationWhileLoop {

  static void printFibonacciElements(int numberOfElements) {

    int firstElement = 0;
    int secondElement = 1;

    System.out.print(firstElement + " " + secondElement);

    int counter = 2;

    while (counter < numberOfElements) {
      int nextElement = firstElement + secondElement;
      System.out.print(" " + nextElement);
      firstElement = secondElement;
      secondElement = nextElement;
      counter++;
    }
  }

  public static void main(String[] args) {

    int numberOfElements = 10;
    printFibonacciElements(numberOfElements);
  }
}

Output : 0 1 1 2 3 5 8 13 21 34 

Explanation

  1. Print the first 2 elements
  2. Initialize the counter element
  3. Start the while loop with an end condition
  4. Add up the 2 elements to generate the next element
  5. Swap the elements (the first element will be swapped with the second element and the second will be swapped with the next element )
  6. Increment the counter
  7. Repeat the 4th, 5th and 6th till the while loop ends.

Print using recursion ( 1 recursive call )

A recursive method calls itself again and again till it encounters the base condition.

package com.fibo;

public class FibonacciRecursion1 {

  static int firstElement = 0;
  static int secondElement = 1;

  static void printFibonacciElements(int numberOfElements) {

    if (numberOfElements > 2) {
      int nextElement = firstElement + secondElement;
      System.out.print(" " + nextElement);
      firstElement = secondElement;
      secondElement = nextElement;
      printFibonacciElements(numberOfElements - 1);
    }
  }

  public static void main(String[] args) {

    System.out.print(firstElement + " " + secondElement);
    int numberOfElements = 10;
    printFibonacciElements(numberOfElements);
  }
}

Output : 0 1 1 2 3 5 8 13 21 34 

Explanation

  1. Print the first 2 elements
  2. Call the print method
  3. Add a base condition to stop the recursive calls
  4. Add up the 2 elements to generate the next element
  5. Swap the elements (the first element will be swapped with the second element and the second will be swapped with the next element )
  6. Call the print method ( this will be a recursive call )
  7. Stop the recursive calls when the base condition is met

Print using recursion ( 2 recursive calls )

package com.fibo;

public class FibonacciRecursion2 {

  static int printFibonacciElements(int counter) {

    if (counter < 2) {
      return counter;
    }

    return printFibonacciElements(counter - 1) + printFibonacciElements(counter - 2);
  }

  public static void main(String[] args) {

    int numberOfElements = 10;

    for (int i = 0; i < numberOfElements; i++) {
      System.out.print(printFibonacciElements(i) + " ");
    }
  }
}

Output : 0 1 1 2 3 5 8 13 21 34 

Explanation

  1. Start the for loop with initialization and end condition
  2. Call the print method with the initialized counter from the for loop and print its output
  3. Add a base condition to stop the recursive calls
  4. Call the recursive print method twice with 2 different counters
  5. Stop the recursive calls when the base condition is met
    -> printFibonacciElements(1) + printFibonacciElements(0) 

    -> 1 + 0 = 1
    - > printFibonacciElements(2) + printFibonacciElements(1) 

    -> printFibonacciElements(2) = printFibonacciElements(1) + printFibonacciElements(0)

    -> printFibonacciElements(1) + printFibonacciElements(0) + printFibonacciElements(1) 

    - > 1 + 0 + 1 = 2
    -> printFibonacciElements(3) + printFibonacciElements(2) 

    -> printFibonacciElements(3) = printFibonacciElements(2) + printFibonacciElements(1)

    -> printFibonacciElements(3) = printFibonacciElements(1) + printFibonacciElements(0) + printFibonacciElements(1)

    -> printFibonacciElements(2) = printFibonacciElements(1) + printFibonacciElements(0) 

    -> printFibonacciElements(1) + printFibonacciElements(0) + printFibonacciElements(1) + printFibonacciElements(1) + printFibonacciElements(0) 

    -> 1 + 0 + 1 + 1 + 0 = 3

Print using dynamic programming

package com.fibo;

public class FibonacciDynamicProgramming {

  static int printFibonacciElements(int counter) {

    int fibonacciArray[] = new int[counter + 2];
    fibonacciArray[0] = 0;
    fibonacciArray[1] = 1;

    for (int i = 2; i <= counter; i++) {
      fibonacciArray[i] = fibonacciArray[i - 1] + fibonacciArray[i - 2];
    }
    return fibonacciArray[counter];
  }

  public static void main(String[] args) {

    int numberOfElements = 10;

    for (int i = 0; i < numberOfElements; i++) {
      System.out.print(printFibonacciElements(i) + " ");
    }
  }
}

Explanation

  • Start the for loop with initialization and end condition
  • Call the print method with the initialized counter from the for loop and print its output
  • Create an array with a specified size and add up the elements based on the condition if required and return the result
-> fibonacciArray[i] = fibonacciArray[i - 1] + fibonacciArray[i - 2];

-> fibonacciArray[2] = fibonacciArray[2 - 1] + fibonacciArray[2 - 2];

-> fibonacciArray[2] = fibonacciArray[1] + fibonacciArray[0];

-> fibonacciArray[2] = 1 + 0 = 1
-> fibonacciArray[i] = fibonacciArray[i - 1] + fibonacciArray[i - 2];

-> fibonacciArray[3] = fibonacciArray[3 - 1] + fibonacciArray[3 - 2];

-> fibonacciArray[2] = fibonacciArray[2] + fibonacciArray[1];

-> fibonacciArray[2] = 1 + 1 = 2

I hope you found this article interesting and valuable. If you are having any concerns or questions about this article please comment below.

Leave a Comment