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

 

5 2 votes
Article Rating
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Niket Shrivastava

Thank You !! It helped me a lot : System.out.println(“BRO !!”);