Java Program to Display Fibonacci Sequence

Fibonacci sequence

Java program to display fibonacci sequence has been shown here. For example if the total no terms is $10$, we get the first $10$ fibonacci numbers i.e. $0, 1, 1, 2, 3, 5, 8, 13, 21, 34$. The following section covers the iterative approach to find fibonacci sequence. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to display fibonacci sequence


1. Take number of terms n as input.

2. Intialize a counter variable say count = 0

3. Assign the first two fibonacci numbers to variables a, b i.e. a = 0 and b = 1

4. Display a and b.

5. Perform t = a + b and display t as next fibonacci.

6. Perform a = b and b = t

7. Perform count = count + 1.

8. If count < n - 2, go to step 5 else stop the process.




2. Pseudocode to display fibonacci sequence


Input : No of terms $n$

Output : First $n$ no fibonacci

1. Procedure fibonacciSequence($n$):

2. $count \leftarrow 0$

3. $a \leftarrow 0$

4. $b \leftarrow 1$

5. Display $a, b$

6. Repeat until $count < n - 2$:

7. $t \leftarrow a + b$

8. Display $t$

9. $a \leftarrow b$

10.$b \leftarrow t$

11.$count \leftarrow count + 1$

12. End Procedure





3. Time complexity to display fibonacci sequence


Time Complexity: O(n)

Where n is the total no of terms in fibonacci sequence.





4. Java Program & output to display fibonacci sequence using iteration

Code has been copied
/**********************************
	alphabetacoder.com
Java program to display fibonacci 
sequence using iteration
***********************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int n, count = 0, a, b, t;

        // take input of the no of terms
        System.out.print("Enter the no of terms = ");
        n = sc.nextInt();

        // intialize the first two number of the sequence
        a = 0;
        b = 1;

        System.out.print("First " + n + " fibonacci numbers: ");

        // display first two fibonacci
        if (n >= 2)
            System.out.print(a + " " + b);
        // display only first fibonacci
        else if (n == 1)
            System.out.print(a);

        //now calculate the remaining n-2 numbers in the sequence
        while (count < n - 2) {
            // calculate next fibonacci
            t = a + b;
            //display next fibonacci
            System.out.print(" " + t);

            //assign values for next iteration
            a = b;
            b = t;

            // increment the counter
            count = count + 1;
        }
    }
}

Output


Enter the no of terms = 20

First 20 fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181





5. Java Program & output to display fibonacci sequence using recursion

Code has been copied
/**********************************
	alphabetacoder.com
Java program to display fibonacci 
sequence using recursion
***********************************/

import java.util.Scanner;

class Main {
    // recursive function to display
    // fibonacci sequence
    static void fibonacci(int a, int b, int count) {
        if (count == 1) // exit condition
            System.out.print(a + " "); // print current term
        else if (count > 1) {
            System.out.print(a + " "); // print current term
            // call function
            fibonacci(b, a + b, count - 1);
        }
    }

    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int n;

        // take input of the no of terms
        System.out.print("Enter the no of terms = ");
        n = sc.nextInt();

        // display n no of fibonacci
        System.out.print("First " + n + " fibonacci numbers: ");
        // call the function
        // pass value of the first two 
        // term and total no of fibonacci
        fibonacci(0, 1, n);
    }
}

Output


Enter the no of terms = 10

First 10 fibonacci numbers: 0 1 1 2 3 5 8 13 21 34