## Free Data Structures and Algorithms Course

### How to print a Fibonacci series on console through Java program ?.

Introduction
Learning Java by example is the most effective approach. Java is very simple and easy programming language, when it comes to code. In order to master Java you should code on daily basis. It can be any code, whether simple java example or advanced java example. Learning by coding will not only give you command over the language but also develop the skills in you to code successfully. Let us start today with a simple and famous program that is : How to print Fibonacci Series in Java ?.
Fibonacci Series : 0, 1, 1, 2, 3, 5, 8, 13, 21, … Here this series starts with 0 and 1 and than each coming digit is the sum of previous two digits. That means it goes as 0+1 = 1, 1+1=2, 1+2 = 3, 2+3= 5 and so on ....In this hub I will be explaining you the code step by step that how a fibonacci number is calculated and printed on the console. The last section would be the code which will demonstrate the working of a fibonacci series.

A closer look at the program stated below :-
Line 1- Import declarations : Java has the ability to reuse the code. There are many predefined classes in Java which we will be using in our fibonacci series program. On line-1 we are importing Scanner class which is been kept in java.util package. Generally to help compiler in locating the class used in the program we use it regularly. Line 3 to 5 - Our Java program on line - 3 begins with a declaration of class named Fibonacci this class is declared as public so that it can be accessible to all Java class. Class Fibonacci contains a main method which is the entry point for every Java program. It is a static method so it is called by the JVM without any objects creation on heap. On next line there is a Java statement which just prints the String written in "". The println method just prints whatever is given to it in double quotes. Here in below example it first ask user , "Enter the number of terms you want from fibonacci series :". It asks user to enter a number to which he wants to see the fibonacci series terms. Fibonacci Series Program Code:-
```import java.util.Scanner;

public class Fibonacci {
public static void main (String[ ] args) {
System.out.println("Enter the number of terms you want from fibonacci series : ");

Scanner scanner = new Scanner(System.in);

int i = scanner.nextInt();

fiboPrint(i);
}
public static final void fiboPrint(long n){

int f0 = 0;
int f1 = 1;

for (int i = 0; i < n; ++i) {
System.out.println(f0 + " ");

final int temp = f1;
f1= f1+ f0;
f0 = temp;
}
}
}
```
Line 7 to 11 :- On line 7, a new Scanner object is created on heap. The Scanner Class is used to read data from a keyboard or disk. Here variable name is scanner and type is Scanner. Here new operator is used to create a physical Scanner object on the heap. This Scanner object is used to read data from the keyboard. System.in passed into constructor of Scanner object generally reads data from the keyboard in bytes. It is the Scanner object which will convert bytes into types such as ints. Line 9 code uses Scanner method nextInt() to obtain user input as Integer, which he types on the keyboard. Generally upon execution of line, the program waits for user to type an int value from keyboard and press enter. Upon pressing enter value types by user gets assigned to int variable called as i. Line 11, calls an static method fiboPrint passing an int i to it. The int value which user entered is passed as an parameter to the method fiboPrint. Line 15 to 16 :- On line 11 fiboPrint method is called and value of int is passed to variable n which has a long data type. On line 15 to 16 we have two local variables by the name f0 and f1 which are int and initialized with value 0 and 1. Line 18 to 23:- On line 18, a for loop statement is written. This for loop has int i as initialization step. Loop also has boolean condition which compares int i to int n passed by the user in fiboPrint method. It has increment expression which increments i by value 1 each time after for loop gets executed. Line 19, prints the value of f0 each time the for loop gets executed. On line21, we create a temp variable who stores value f1. On line 22, f1 is assigned a value which is addition of f1 and f0. On line 23, we assign value temp to variable f0. If the user enters number as 5, the output would be 0, 1, 1, 2, 3, etc ....

© 2021 Learn Java by Examples Template by Hubberspot