Write a Java Program to display Fibonacci Series using While Loop

By | 20.01.2017

Java Program to display Fibonacci Series using While Loop


Fibonacci series is in the form of 1, 1, 2, 3, 5, 8, 13, 21,……

To find this series we add two previous terms/digits and get next term/number.

 

SOURCE CODE ::

import java.util.Scanner;

public class Fibonacci {

  
    public static void main(String[] args) {
        
        int a=1,b=1,n,fibon=1;
              
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter how many terms u want : ");
        n=sc.nextInt();
        System.out.print(fibon);
        while(n>1)
        {
             System.out.print(" "+fibon);
             fibon=a+b;
             a=b;
             b=fibon;
             n--;
        }
         System.out.println("");
        
    }
    
}

 

OUTPUT ::

 

Enter how many terms u want : 
11
1 1 2 3 5 8 13 21 34 55 89

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments