Social Icons

Tuesday, 11 February 2014

Java program print the Fibonacci series up to that given range

import java.io.*;
class Fabonic
{
public static void main(String args[])throws IOException
     {
             int n=0,a=-1,b=1,sum=0,check;
             System.out.print("\n Enter the value : ");
             BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
                  try
                     {
                                n=Integer.parseInt(br.readLine());
                      }

                   catch(Exception e)
                    {
                                System.out.println(" The Input is wrong");
                     }
                                check=n;
                                System.out.print("\n Ascending Order : ");
                                while(n>=0)
                                {
                                               sum=a+b;
                                                a=b;
                                                b=sum;
                                                System.out.print(sum+"  ");
                                                n--;
                                }
                                System.out.println("\n");
                                System.out.print(" Descending Order : ");
                                while(check>=0)
                                {        
                                               sum=b;
                                                b=a;
                                                a=sum-b;
                                                System.out.print(sum+"  ");
                                                check--;
                                }
                                System.out.println("\n");
                }
}



                                                              Output



No comments:

Post a Comment