How to calculate and print fibonacci series on the console through a Java program

Updated on September 17, 2014

The code which I am about to share with you is a Java snippet which prints Fibonacci series on the console of your command line. It reads number of terms from console. After calculating Fibonacci series, it prints result on the console. In this simple program, there is a class named as 'FibonacciSeriesDemo'. It contains method named as 'public static void main(String [] args)'. This method is been called internally by Java Virtual Machine. It is static and public in nature. It does not return any value. Itâ€™s entry point for any Java program to execute. Here is the code :

```import java.util.Scanner;

class Fibonacci {
public void fibonacci( int terms ) {

// 4. Creating two variable for
// storing first two values of fibonacci
// series, that is 0 and 1
int firstNumber = 0;
int secondNumber = 1;

// 5. Looping through each terms and printing
// value of number 1
for (int i = 0; i < terms; i++ ) {
System.out.println(firstNumber + " ");

// 6. storing number 2 in temp so that on next term
// added value should be displayed which outputs to
// number 1 + number 2
int temp = secondNumber;
secondNumber= secondNumber+ firstNumber;
firstNumber = temp;

}
}
}

public class FibonacciSeriesDemo {

public static void main(String[] args) {

// 1. We will be creating an object Scanner class.
// This object will be created by passing System.in
// object to it. It will be responsible for reading
// values from the console which user enter
Scanner scanner = new Scanner(System.in);

System.out.println("Enter number of terms for fibonacci series ");

// 2. Storing the number of terms entered by user in a variable
// terms by calling nextInt method of Scanner which reads a value
// from the console
int terms = scanner.nextInt();

// 3. Creating a Fibonacci object and calling
// fibonacci method by passing terms as a parameter
Fibonacci fibonacci = new Fibonacci();
fibonacci.fibonacci(terms);

}

}
```

Steps to implement

Step 1 :- Create an object of Scanner class. This object will be created by passing System.in as an argument to constructor. It will be responsible for reading values from the console which user enter.

Step 2 : - Storing the number of terms entered by user in a variable terms by calling nextInt method of Scanner which reads a value from the console.

Step 3 :- Creating a Fibonacci object and calling fibonacci method by passing terms as a parameter.

Step 4 :- Creating two variable for storing first two values of fibonacci series, that is 0 and 1.

Step 5 :- Looping through each terms and printing value of number 1.

Step 6 :- Storing number 2 in temp so that on next term added value should be displayed which outputs to number 1 + number 2.

In order to make this tutorial more understandable, I am providing a video tutorial for it below. This video tutorial will give you a walk through for all the steps performed in above java code.

In this video fibonacci series is calculated and displayed on the console.

How to print Fibonacci series on the console ?.

Output of the program -

See results

0

12

9

0

0

0

1