Fibonacci Series Program in Java: A Comprehensive Guide

TechDyer

The Fibonacci series usually begins with 0 and 1. In this sequence, each number is the total of the two numbers that came before it. It illustrates a well-known mathematical series frequently used in programming and computer science. This post will explain the recursive and iterative methods for writing a Fibonacci Series Program in Java.

What is the Fibonacci Series?

Italian mathematician Leonardo Fibonacci described this Fibonacci sequence in his book “Libre Abachi.” This sequence is mentioned in some Sanskrit texts as well. This pattern is even present in flowers. The numbers in the Fibonacci series begin at 0 and go up to 1. The sum of the next two numbers is applied.

An Iterative Method for the Fibonacci Series Program in Java

Using the first two terms, 0 and 1, from the Fibonacci series, we will iteratively find additional terms by using these two terms. Let’s examine how to use this technique to write the Java Fibonacci series program.

 

Code:

// iterative fibonacci program in java

class PrepBytes {

    public static void main(String[] args) {

        int first_term=0;

        int second_term=1;

        int second_last_term=0;

        int last_term=1;

        System.out.println(“The Fibonacci series’ first ten terms are:”);

        for(int i=0;i<10;i++){

            if(i==0){

                System.out.print(first_term+” “);

            }

            else if(i==1){

                System.out.print(second_term+” “);

            }

            else{

                int current_term=second_last_term+last_term;

                System.out.print(current_term+” “);

                second_last_term=last_term;

                last_term=current_term;

            }

        }

    }

}

 

See also  Message Blocking Active: Solutions and Alternatives

Output: 

The Fibonacci series’ first ten terms are:

0 1 1 2 3 5 8 13 21 34 

Recursive method for the Fibonacci series program in Java

The recursive formula for the nth term in this method is (n-1) + (n-2), with the first two terms coming from the base case. Let’s look at the recursive method of writing a Java program for the Fibonacci sequence.

 

Code:

// recursive fibonacci program in java

class PrepBytes {

    static int fibonacci(int n)

    {

        if (n < 2)

            return n;

 

        return fibonacci(n – 1)

            + fibonacci(n – 2);

    }

    public static void main(String args[])

    {

        System.out.println(“The first 10 terms of the Fibbonaci series are:”);

        for (int i = 0; i < 10; i++) {

 

            System.out.print(fibonacci(i) + ” “);

        }

    }

}

 

Output:

The Fibonacci series’ first ten terms are:

0 1 1 2 3 5 8 13 21 34 

Fibonacci  Series Program In Java Using Scanner

Code: 

import java. util.Scanner;  public class FibonacciSeries {

 public static void main(String[] args) {

 Scanner scanner = new Scanner(System.in);

 System. out.print(“Enter the number of terms in the Fibonacci series: “);

 int n = scanner.next();

 scanner.close();  // Print the Fibonacci series

 System. out.println(“Fibonacci Series:”);

 int prev = 0;

 int curr = 1;

 for (int i = 0; i < n; i++) {

 System.out.print(prev + ” “);

 int next = prev + curr;

 prev = curr;

 curr = next;

 }

 }

}

 

Output:

Enter the number of terms in the Fibonacci series: 10

Fibonacci Series:0 1 1 2 3 5 8 13 21 34

Recursion + Memoization method for the Fibonacci series program in Java

The memoization method will be used in conjunction with the above recursion method, which is the only difference in this approach. By memorizing a term, we avoid having to constantly search for its definition and instead store the answers to all of the calculated terms for later use.

See also  Meesho Upcoming Sale 2024: Best Deals & Offers

 

Code:

// recursive + memoization fibonacci program in java

class PrepBytes {

    static int fibonacci(int n,int[] memo)

    {

        if (memo[n]!=-1){

            return memo[n];

        }

        if (n < 2)

            return n;

 

        int current_term= fibonacci(n – 1,memo) + fibonacci(n – 2,memo);

        memo[n]=current_term;

        return current_term;

    }

    public static void main(String args[])

    {

        int[] memo= new int[10];

        for(int i=0;i<10;i++){

            if(i<2){

                memo[i]=i;

            }    

            else{

                memo[i]=-1;

            }

        }

        

        System.out.println(“The first 10 terms of the Fibbonaci series are:”);

        for (int i = 0; i < 10; i++) {

 

            System.out.print(fibonacci(i,memo) + ” “);

        }

    }

}

Output:

The Fibonacci series’ first ten terms are:

0 1 1 2 3 5 8 13 21 34

Conclusion

This tutorial examines the Fibonacci sequence and offers several Java implementation techniques. Iterative, recursive, and memoization techniques allow programmers to efficiently produce Fibonacci sequences. These methods benefit both novice and expert programmers by showcasing the mathematical elegance of the series and its usefulness in computer science.

Read more

Share This Article
Follow:
I'm a tech enthusiast and content writer at TechDyer.com. With a passion for simplifying complex tech concepts, delivers engaging content to readers. Follow for insightful updates on the latest in technology.
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *