# Java Program To Make Fibonacci Series With Explanation

So, you are here to find the 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 follow this rule

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

and **the series starts with 0,1 **which are initialized terms.

You have understand the sequence. Now its time to go for the Java program.

You can use these algorithm in other programming languages too.

Make A Multiplication Table Using Java Program

Java program to make Fibonacci Series

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+",\n"+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 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 have made a Scanner named ‘scan’

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

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

System.out.println(a+”,\n”+b+”,”);

\n is used to go to the next line and + is used to concatenate these.

Then we run the for loop where we have initialized i=0 and the loop will run until the value of i reach to the value of n-2.

think for a while why we have written n-2 ? just 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;

and then printed the value of c in the for loop.

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

here are some screenshots :

Output:

happy coding 🙂

## Leave a Reply

You must be logged in to post a comment.