Java Program to Display Fibonacci Series

Java Program to Display Fibonacci Series

What is a Fibonacci Series ?

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, beginning with 0 and ending with 1. The number sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

Steps to Find the Factorial of a Number : 

  1. Create two variables, “a” and “b”, and set their values to 0 and 1.
  2. Then, add “a” and “b” and store the sum in “b”.
  3. And update “a” with the previously calculated value of “b”.
  4. Repeat this process n times and store each new value of “b” in a list to obtain the Fibonacci series.

Code example for the above algorithm :

public static void fibonacci(int n) {
    int a = 0, b = 1;
    System.out.print(a + " " + b);

    for (int i = 2; i < n; i++) {
        int c = a + b;
        System.out.print(" " + c);
        a = b;
        b = c;
    }
}

Code example for recursion method :

function fibonacci(n)
    if n <= 1
        return n
    else
        return fibonacci(n-1) + fibonacci(n-2)

Example : Using a for loop 

Run

public class Main {
    public static void main(String[] args) {
        int n = 10;
        int a = 0, b = 1;
        System.out.print(a + " " + b);

        for (int i = 2; i < n; i++) {
            int c = a + b;
            System.out.print(" " + c);
            a = b;
            b = c;
        }
    }
}

Output :

0 1 1 2 3 5 8 13 21 34

Explanation : 

This program generates the Fibonacci sequence up to n, where n in this example is set to 10. It begins by declaring and initializing the variables n, a, and b. It then prints the first two numbers in the sequence, 0 and 1. Following that, it employs a for loop to calculate and print the next n-2 numbers in the sequence. The program accomplishes this by adding the sequence’s two previous numbers, assigning the sum to the variable c, and then setting a to b and b to c. It then prints c and continues this process until the for loop is finished.

Example : Using recursion

Run
public class Main {
    public static int fibonacci(int n) {
        if (n <= 1) {
            return n;
        } else {
            return fibonacci(n-1) + fibonacci(n-2);
        }
    }

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

Output :

0 1 1 2 3 5 8 13 21 34 

Explanation :

In this example, The fibonacci() function is a recursive function that calculates the nth term of the Fibonacci series. It takes an integer n as an argument and returns an integer. If n is 0 or 1, it returns n as the result. If n is greater than 1, it returns the sum of the n-1th term and the n-2th term of the Fibonacci series. The calculation is done by calling the fibonacci() function recursively with n-1 and n-2 as arguments.

Example : Using while loop 

Run
public class Main {
    public static void main(String[] args) {
        int n = 10;
        int a = 0, b = 1;
        int c;
        System.out.print(a + " " + b);

        int i = 2;
        while (i < n) {
            c = a + b;
            System.out.print(" " + c);
            a = b;
            b = c;
            i++;
        }
    }
}

Output :

0 1 1 2 3 5 8 13 21 34

Explanation :  

  • In the above code, we first declare and initialize three integer variables a, b, and c. a and b have initial values of 0 and 1, respectively, and c has not yet been initialized. The initial values of a and b are then printed to the console.
  • Following that, we declare and assign the integer variable i the value 2. I’s purpose is to keep track of how many terms we’ve printed so far.
  • The while loop will continue to run as long as i is less than n. In each loop iteration, we calculate and store the next term of the Fibonacci series, which is the sum of a and b. After that, we print c to the console. Following that, we update the values of a and b to be b and c, respectively. Finally, we increment the value of i by 1.
  • We have found and printed the first n terms of the Fibonacci series after the while loop has completed. The program’s output will be the same as in the previous example: 0 1 1 2 3 5 8 13 21 34.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription