Fibonacci Series in Java Programming Language

If you’ve ever had a programming interview, you were probably aware of the many Java programming interviews that could include some questions like to make a program for the Fibonacci series in java. And that’s why this question could bother many candidates. Also, many people are perplexed by this seemingly basic question. In this article, we will put a light on how to implement the Fibonacci series in Java.

Fibonacci Series in Java

The Fibonacci series is a series of some specific number paced by adding the antecedent two numbers in the series. The first two numbers are zero and one that comes simultaneously. The number that comes after these two terms are generated by generally adding the last two numbers.

How to Write a Fibonacci Series Program in Java:

There are two major methods to write the Fibonacci series program:

  • Fibonacci Series without recursion
  • Fibonacci Series using recursion in java

How to calculate Fibonacci?

The Fibonacci sequence included the series of specific numbers such as 0,1,1,2,3,5,8,13,21,34,

And we can get the next number by adding up the last two numbers:

  • 2 is calculated by summing the two numbers antecedent it (1+1),
  • 3 is calculated by summing the two numbers antecedent it (1+2),
  • 5 is (2+3), and so on…

Now take a closure look and watch the example of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, and 13….etc.

Also Read: Bubble Sort Program in Java Language

In the above-mentioned example, zero and one are the first two numbers of the series. These two numbers are printed directly. The third number is created by summing the first two numbers. In such cases zero and one. So, we get 0+1=1. Hence one is printed as the third number. The next number is acquired by using the second and third numbers and not using the first number. This is all done until the number of terms you want or demanded by the user. In the above-mentioned example, we have used a series of eight numbers.

Example 1: Display Fibonacci Series Using for Loop

class Main {

  public static void main(String[] args) {

    int n = 10, firstTerm = 0, secondTerm = 1;

    System.out.println("Fibonacci Series till " + n + " terms:");

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

      System.out.print(firstTerm + ", ");

      // compute the next term

      int nextTerm = firstTerm + secondTerm;

      firstTerm = secondTerm;

      secondTerm = nextTerm;

    }

  }

}

Output

Fibonacci Series till 10 terms:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2: Display Fibonacci Series Using while loop

class Main {

  public static void main(String[] args) {

    int i = 1, n = 10, firstTerm = 0, secondTerm = 1;

    System.out.println("Fibonacci Series till " + n + " terms:");

    while (i <= n) {

      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;

      firstTerm = secondTerm;

      secondTerm = nextTerm;

      i++;

    }

  }

}

Output

Fibonacci Series till 10 terms:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Fibonacci Series Program in Java Without Recursion

class Fibonacci Example1{ 

public static void main(String args[]) 

{   

 int n1=0,n2=1,n3,i,count=10;   

 System.out.print(n1+" "+n2);//printing 0 and 1    

 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed   

 {   

  n3=n1+n2;   

  System.out.print(" "+n3);   

  n1=n2;   

  n2=n3;   

 }   

}}

Output

0 1 1 2 3 5 8 13 21 34

Fibonacci Series Program in Java Using Recursion

class Fibonacci Example2{ 

 static int n1=0,n2=1,n3=0;   

 static void printFibonacci(int count){   

    if(count>0){   

         n3 = n1 + n2;   

         n1 = n2;   

         n2 = n3;   

         System.out.print(" "+n3);  

         printFibonacci(count-1);   

     }   

 }   

 public static void main(String args[]){   

  int count=10;   

  System.out.print(n1+" "+n2);//printing 0 and 1   

  printFibonacci(count-2);//n-2 because 2 numbers are already printed  

 } 

}

Output

0 1 1 2 3 5 8 13 21 34

Conclusion:

That’s all about the Fibonacci series in java. If you still have any questions, you can ask us in the comment box.