Fibonacci series in java

Let’s learn fibonacci series in java.

fibonacci series in java

Fibonacci series in java

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

For example: 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . .

Logic

Let’s understand fibonacci series logic.

  • Initially first two numbers in fibonacci series are fixed. That is, 0 and 1.
  • Next number in the fibonacci series will be the summation 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 FibonacciDemo
{
   public static void main(String[] args) 
   {
      int a = 0;
      int b = 1;
      System.out.println(a + " " + b);
      int number;
      for(int x = 1; x <= 10; x++)
      {
         number = a + b;
         System.out.println(" " + number);
         a = b;
         b = number;
      }
   }
}


Output:

0 1 1 2 3 5 8 13 21 34 55 89


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.

// java fibonacci
public class FibonacciExample
{
   public static void main(String args[])
   {
      int a = 1, num = 15, first = 0, second = 1;
      System.out.print("First " + num + " series : ");
      while(a <= num)
      {
         System.out.print(first + " , ");
         int total = first + second;
         first = second;
         second = total;
         a++;
      }
   }
}


Output:

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


using recursion

Similarly here’s the fibonacci recursive or fibonacci series in java using recursion,

// fibonacci series
public class FibonacciRecursion 
{
   static int num1 = 0, num2 = 1, num3 = 0; 
   static void fibonacciRecursive(int count)
   { 
      if(count > 0)
      { 
         num3 = num1 + num2; 
         num1 = num2; 
         num2 = num3; 
         System.out.print(" " + num3);
         // fibonacci recursion
         fibonacciRecursive(count - 1); 
      } 
   }

   public static void main(String[] args) 
   {
      int number = 12; 
      System.out.print(num1 + " " + num2); 
      fibonacciRecursive(number - 2);
   }
}


Output:

0 1 1 2 3 5 8 13 21 34 55 89


Also read – variables in java


using scanner

To display fibonacci series using scanner first read input from user using sc.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 is equal to ‘c’. Else nextNumber is equal to a plus b.

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 FibonacciSeriesExample 
{
   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 using scanner: ");
      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 using scanner:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

sum of

Now let’s learn how to write program to find the sum of fibonacci series.

public class SumFibonacciSeries
{
   static int sumFibonacciSeries(int number) 
   { 
      if(number <= 0)
      {
         return 0;
      }
      int[] arrFibonacci = new int[number + 1];
      // initializing
      arrFibonacci[0] = 0; 
      arrFibonacci[1] = 1;
      int total = arrFibonacci[0] + arrFibonacci[1];
      for(int a = 2; a <= number; a++) 
      { 
         arrFibonacci[a] = arrFibonacci[a - 1] + arrFibonacci[a - 2]; 
         total += arrFibonacci[a]; 
      }
      return total; 
   }

   public static void main(String[] args) 
   {
      int num = 10; 
      System.out.println("Sum of fibonacci series is : " + sumFibonacciSeries(num));
   }
}


Output:

Sum of fibonacci series is : 143