Thursday, January 2, 2014

Fibonacci & Non-Fibonacci Series



The Fibonacci Series is a sequence of numbers first created by Leonardo Fibonacci. It is strange but true that leaves and flower arrangement of plants follows the pattern of the Fibonacci numbers. Sunflower and some pine cones show these numbers.

Fibonacci series  start with 0 and 1 and the next numbers found by adding the previous two numbers. First 10 Fibonacci numbers are:

0 1 1 2 3 5 8 13 21 54



import java.io.*;
    class Fibo_NonFibo
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    System.out.print("\tEnter a No : ");
                    n=Integer.parseInt(Br.readLine());
                    Fibo_NonFibo Fb=new Fibo_NonFibo();
                    System.out.print("\n\tFirst "+n+" Fibonacci Series ");
                    Fb.Fibo(n);
                    System.out.print("\n");
                    System.out.print("\n\tFirst "+n+" Non Fibonacci Series ");
                    Fb.NonFibo(n);
                   
                   
                }
           void Fibo(int n)
                {
                    int a=1,b=0,c=0;
                    while(c<=10)
                        {
                            System.out.print("  "+c);
                            c=a+b;
                            a=b;
                            b=c;
                        }
                }
           void NonFibo(int n)
                {
                    int a=1,b=0,c=0;
                                     
                    while(c<=10)
                        {
                            c=a+b; 
                            for(int i=b+1;i<c;i++)
                                {
                                    System.out.print("  "+i);
                                }
                            a=b;
                            b=c;
                        }
                }               
        }




No comments: