You are here:   HomeJava ProgrammingControl Structures in JavaIf Else If Ladder, if..else ....if else statement in Java Example
by Dinesh Thakur Category: Control Structures

Fibonacci series numbers are in the following Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144..., The first two number in Fibonacci series are 0 and 1, and each subsequent next number is the sum of the previous two Numbers.

Algorithm for Fibonacci Series Numbers:-

step 1:Set a=0,b=1,c=0

step 2: Read size

step 3: Print a,b

step 4: Repeat through step-8 until (c < = size)

step 5: c=a+b

step 6: If (c < =size) then print c

step 7: Set a=b

step 8: Set b=c

step 9: Exit

Here is the Java Example for Fibonacci Series Numbers:

import java.util.Scanner;

public class FibonacciSeries
{
                 public static void main(String args[])
            {
                      int a=0,b=1,c=0,size;
                 Scanner s=new Scanner(System.in);
                 System.out.print("Enter a range : ");
                 size=s.nextInt();
                 System.out.print("\n");
                 System.out.print(a+","+b+",");
                 while(c<=size)
                       {
                          c=a+b;
                          if(c<=size)
                            System.out.print(c+",");
                            a=b;
                            b=c;
                       }
               }
}

Fibonacci Series in Java



What's New and Popular