Fibonacci Series Program in Java

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…

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

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
         System.out.print(t1+"\t");

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

         // increase iterator variable
         // value by 1
         i++;
      }
   }

   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: ");
      displayFibonacci(terms);

      // close Scanner class object
      scan.close();
   }
}

The output for different test-cases:-

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

Enter number of terms: 10
The Fibonacci series:
0 1 1 2 3 5 8 13 21 34

The above Fibonacci series program in Java is developed by using a while loop. We can also use for loop to find the Fibonacci series.

Using for loop

public static void displayFibonacci(int term) {

	int t1 = 0; // first term
	int t2 = 1; // second term	
	int n; // nth term

	for(int i=1; i <= term; i++) {

		// display ith terms
		System.out.print(t1+"\t");

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

Fibonacci series program in Java up to a given number

We can also display the Fibonacci series up to a given number. In this case, again we can use any looping statement like for loop or while loop or do-while loop.

import java.util.Scanner;

public class FibonacciSeries {

   public static void displayFibonacci(int range) {

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

      while(t1 <= range) {

	 //display the term
	 System.out.print(t1+"\t");

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

   public static void main(String[] args) {
      int range; // range value

      // Read range value
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter range: ");
      range = scan.nextInt();

      // display Fibonacci series
      System.out.println("The Fibonacci series"
      		+ " upto "+range+" is: ");
      displayFibonacci(range);

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter range: 20
The Fibonacci series up to 20 is:
0 1 1 2 3 5 8 13

Enter range: 100
The Fibonacci series up to 100 is:
0 1 1 2 3 5 8 13 21 34 55 89

Enter range: 1000
The Fibonacci series up to 1000 is:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

Finding Nth Fibonacci term in Java

import java.util.Scanner;

public class FibonacciSeries {

   public static int nthFibonacciTerm(int term) {

      int t1 = 0; // first term
      int t2 = 1; // second term
      int n = 0; // nth term

      if(term==1) return t1;
      if(term==2) return t2;

      // 2 terms are already exist so 
      // iterator variable should 
      // start with 3
      for(int i=3; i <= term; i++) {

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

      return n;
   }

   public static void main(String[] args) {

      int n; // range value

      // read range value
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter n value: ");
      n = scan.nextInt();

      // display Fibonacci series
      System.out.println(n+" Fibonacci term "
		+ " is = "+ nthFibonacciTerm(n));

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter n value: 1
1 Fibonacci term is = 0

Enter n value: 2
2 Fibonacci term is = 1

Enter n value: 7
7 Fibonacci term is = 8

Enter n value: 10
10 Fibonacci term is = 34

We can also find the Fibonacci series using the recursion technique. For more see:- Fibonacci series using recursion

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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