Fibonacci series in java

Let’s learn fibonacci series in java.

Fibonacci series in java

A fibonacci series is a sequence where next number is sum of previous two numbers.

fibonacci series in java

fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . .


algorithm for fibonacci series

Here’s algorithm for fibonacci series.

  • Initially first two numbers in fibonacci series are fixed. That is, 0 and 1.
  • Next number in the fibonacci series will be the sum of previous two numbers and so on. The fibonacci series continues.
  • Now the logic here is first create two variables ‘a’ and ‘b’ having values 0 and 1. Also create variable int number.
  • Then in the for loop iterate variable ‘i’ equal to one to ‘i’ less than or equal to 10 (for loop executing 10 times).
  • now ‘number’ is equal to ‘a’ plus ‘b’. Meanwhile print ‘number’ on the console using System.out.println
  • now assign value ‘b’ to ‘a’ and value ‘number’ to ‘b’. By doing this we are swapping values inside for loop.

Let’s see java program to display fibonacci series using for loop.

public class FibonacciSeriesForLoop
{
   public static void main(String[] args)
   {
      int f = 10, num1 = 0, num2 = 1;
      System.out.print("First " + f + " fibonacci sequence: ");
      for(int a = 1; a <= f; ++a)
      {
         System.out.print(num1 + " + ");
         int total = num1 + num2;
         num1 = num2;
         num2 = total;
      }
   }
}


Output:

First 10 fibonacci sequence: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +


fibonacci series using while loop

In the same way we can use while loop to display fibonacci series.

Here’s the fibonacci series in java using while loop.

public class FibonacciSeriesWhileLoop
{
   public static void main(String[] args)
   {
      int a = 1, num = 15, num1 = 0, num2 = 1;
      System.out.print("First " + num + " fibonacci sequence: ");
      while(a <= num)
      {
         System.out.print(num1 + " , ");
         int total = num1 + num2;
         num1 = num2;
         num2 = total;
         a++;
      }
   }
}


Output:

First 15 fibonacci sequence: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 ,


fibonacci using recursion

Meanwhile here’s the fibonacci series program in java using recursion.

public class FibonacciRecursion
{
   static int num1 = 0, num2 = 1, num3 = 0;
   public static void main(String[] args)
   {
      System.out.print(num1 + " " + num2);
      FibonacciRecursion obj = new FibonacciRecursion();
      obj.fibonacciRecursive(14);
   }
   void fibonacciRecursive(int f)
   {
      if(f > 0)
      {
         num3 = num1 + num2;
         System.out.print(" " + num3);
         num1 = num2;
         num2 = num3;
         // fibonacci recursion
         fibonacciRecursive(f - 1);
      }
   }
}


Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610


Also read – variables in java


fibonacci series in java using scanner

To display fibonacci series first read total number to display fibonacci series from user using nextInt() method of Scanner class.

Next in for loop iterate variable ‘c’ from 0 to c < num. Inside for loop check if ‘c’ is less than or equal to 1.

If it is less than or equal to 1 then ‘nextNumber’ variable is equal to ‘c’. Else ‘nextNumber’ variable is equal to ‘a’ plus ‘b’.

Also read – continue statement in java

Now assign value of variable ‘b’ to variable ‘a’ and assign value of variable ‘nextNumber’ to variable ‘b’.

Lastly print or dislay fibonacci series on console. Here’s the example on fibonacci series in java using scanner.

import java.util.Scanner;
public class FibonacciSeriesUsingScanner 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter total number to display fibonacci series: ");
      int num = sc.nextInt();
      int a = 0, b = 1, nextNumber;
      System.out.println("Fibonacci series: ");
      for(int c = 0; c < num; c++)
      {
         if(c <= 1)
         {
            nextNumber = c;
         }
         else
         {
            nextNumber = a + b;
            a = b;
            b = nextNumber;
         }
         System.out.println(nextNumber);
      }
      sc.close();
   }
}


Output:

Please enter total number to display fibonacci series: 20
Fibonacci series:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181