Fibonacci series in java



www.java67.com
In mathematics, the Fibonacci numbers orFibonacci series or Fibonacci sequence are thenumbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144... By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
 import java.util.Scanner;  
 public class PracticeDemo {  
      public static void getFibonacciSeries(int number){  
           int count=2,first=0,second=1,third=0;  
           System.out.print(first+" "+second);  
           if(number>0)  
           do{  
                third=first+second;  
                first=second;  
                second=third;  
                count++;  
                System.out.print(" "+third);  
           }while(count<number);  
      }  
      public static void main(String [] args){  
           Scanner sc=new Scanner(System.in);  
           System.out.println("Enter the number");  
           int x=sc.nextInt();  
           getFibonacciSeries(x);  
           sc.close();  
      }  
 }  


Output:Enter the number : 10
0
1
1
2
3
5
8
13
21
34


Comments

Popular posts from this blog

Reasoning-Number Series

Reasoning-Letter Series

Profit and Loss