Print Fibonacci Series in Java



The Fibonacci Series generates subsequent numbers by adding two previous numbers. The Fibonacci series starts from two numbers ? F0 & F1. The initial values of F0 & F1 can be taken as 0, 1, or 1, 1 respectively.

Fn = Fn-1 + Fn-2

Problem Statement

Write a program in Java program to print a Fibonacci series.

Input

Run the program

Output

1 1 2 3 5 8 13 21 34 55

The above output is obtained when the values of n=10.

Different approaches to print a Fibonacci series

Following are the different approaches to printing a Fibonacci series ?

Using bottom-up dynamic programming

Below are the steps to print a Fibonacci series using dynamic programming ?

  • Initialize the variables
  • Check for edge cases
  • Iterate to calculate the terms
  • Update the variable 
  • Print the result.

Example

Below is the Java program to print a Fibonacci series using bottom-up dynamic programming ?

public class FibonacciSeries {
  public static void main(String args[]) {
    int a, b, c, i, n;
    n = 10;
    a = b = 1;
    System.out.print(a + " " + b);
    for (i = 1; i <= n - 2; i++) {
      c = a + b;
      System.out.print(" ");
      System.out.print(c);
      a = b;
      b = c;
    }
  }
}

Output

1 1 2 3 5 8 13 21 34 55

Code Explanation

Initialize two variables, last and secondLast, to store the (i-1)th and (i-2)th terms. Loop from 2 to n to compute each i-th term as the sum of last and secondLast. Update secondLast to last and last to the current i-th term in each iteration. This approach efficiently generates the Fibonacci sequence up to the n-th term.

Time complexity : O(n)
Space Complexity : O(1)

Using Recursive approach

Below are the steps to print a Fibonacci series using the Recursive approach ?

  • First in the main method, set n = 10 since we want to print the first 10 numbers in the Fibonacci series.
  • We use a for loop to go through numbers from 0 to n - 1 (or 9). In each loop, we call the Fibonacci method to find the Fibonacci number at that position and print it.
  • Calculate Fibonacci with Recursion and inside the Fibonacci method if the position (n) is 0 or 1, we return n directly. This is because the first two numbers in the Fibonacci series are both 1.
  • For any other position, we find the number by adding up the two previous numbers in the sequence. We do this by calling Fibonacci(n - 1) and Fibonacci(n - 2) and adding their results together.

Example

Below is the Java program to print a Fibonacci series using the Recursive approach ?

public class FibonacciRecursive {
    public static void main(String[] args) {
        int n = 10;
        for (int i = 0; i < n; i++) {
            System.out.print(fibonacci(i) + " ");
        }
    }
    public static int fibonacci(int n) {
        if (n <= 1) {
            return n;
        }
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

Output

0 1 1 2 3 5 8 13 21 34 

Code Explanation

The above code calculates the Fibonacci series recursively by calling Fibonacci for each position from 0 to 9. The method adds up the results from the two previous positions to get each new number, except for the first two positions, which are 1.

Time complexity : O(2n)

Space Complexity : O(1)

Updated on: 2024-11-18T22:31:46+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements