Home Lifestyle Fibonacci Series Program in Java Language

Fibonacci Series Program in Java Language


Fibonacci series in Java | In the Fibonacci series, the next element will be the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on…

<img decoding=

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

As a rule, the expression is Xn= Xn-1+ Xn-2

Fibonacci Series Program in Java Language

import java.util.Scanner;

public class FibonacciSeries {

   public static void displayFibonacci(int term) {

      // declare variables
      int t1 = 0; // first term
      int t2 = 1; // second term	
      int n; // nth term
      int i = 1; // iterator variable

      while(i <= term) {

         // display ith terms

         // update term values
         n = t1 + t2;
         t1 = t2;
         t2 = n;

         // increase iterator variable
         // value by 1

   public static void main(String[] args) {

      int terms; // number of terms

      // Read number of terms to display
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter number of terms: ");
      terms = scan.nextInt();

      // display Fibonacci series
      System.out.println("The Fibonacci series: ");

      // close Scanner class object


Enter number of terms: 7
The Fibonacci series:
0 1 1 2 3 5 8

Types of Sorting Algorithms:

Additional Reading


If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 😊 Keep Learning !



Please enter your comment!
Please enter your name here