Java Program to Print Fibonacci Series

So, you are here to find a suitable program for you to display the Fibonacci series.

What is Fibonacci series?

Fibonacci Series is one kind of series in mathematics. It is also known as Fibonacci sequence.
The sequence looks like below :
0,1,1,2,3,5,8,13,21,34,55,89,144,…… and so on

If you try to understand the protocol of this sequence you can easily get it.
Just notice carefully the first two numbers that are zero and one. Then 1 comes. That means the third term is nothing but the summation of the previous two terms.

[first term+second term]=[third term]

i,e

[0+1]=[1]

Then,

[second term+third term]=[fourth term]

i,e

[1+1]=[2]

and it goes with the same protocol

We can say that the Fibonacci series follows this rule

[addition of last two numbers]=[next number]

and the series starts with 0,1 which are initialized terms.
Now it’s time to go for the Java program.

Also check: Make A Multiplication Table Using Java Program

Print Fibonacci Series in Java

import java.util.Scanner;
public class fibonacci{

 public static void main(String[] args){
    Scanner scan=new Scanner (System.in);
    int n=scan.nextInt();
    int a=0,b=1;
    int c;
    System.out.println(a+","+b+",");
    for(int i=0;i<n-2;i++)
    {
      c=a+b;
      a=b;
      b=c;
      System.out.println(c+",");
    }

    
    
}
}

Explanation of this program:

import java.util.Scanner;

This line will import the Scanner util package, so that we can use Scanner in our program to set the range or set how many terms we are gonna print in our series.

At first, we made a Scanner named ‘scan’

and take a variable n where the value of n will be inserted by the user during run time.

Then we have taken another two integers a and b. These values are the first two numbers of our sequence so we printed it first using these lines

System.out.println(a+","+b+",");

You can use \n if you want to print it in a newline.

Then we run the for loop where we have initialized i=0 and the loop will run until the value of i reaches the value of n-2.
We have used n-2 because we have previously printed the first two terms manually.

So if a user intends to get the Fibonacci series of 18 terms then we need to get the 16 next terms after the first two terms. (2+16=18)

now look into the for loop block

{
c=a+b;
a=b;
b=c;
System.out.println(c+",");
}

We have taken an integer c previously. Now just assume that c will be the next term. So we just added a and b and stored the result into c

And set the second term b as the first term of last two terms now by using this a=b;

And set third term c as the second term by using b=c;

Then printed the value of c in the for loop.

It will print the value of the addition of the last two terms continuously until it reaches the desired length of terms.

2 responses to “Java Program to Print Fibonacci Series”

  1. T.Aparna says:

    Explanation of this program is too good, and it helps a lot to me.Thankyou.

Leave a Reply

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