Arnab_Manna

fib_range.java

Dec 7th, 2021
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;//java package
  2. class fibo
  3. {//class starts
  4.     void fib(int n,int m)// method fib
  5.     {
  6.         int f1=0,f2=1,f3=1;
  7.          if(n==0 ){
  8.                         System.out.print("0 ,1,");
  9.                     }
  10.         while(f1<=m){
  11.                 f1=f2;
  12.                 f2=f3;
  13.                 if(f3>=n && f3<=m){
  14.               System.out.print(f3+",");}
  15.                 f3=f1+f2;      
  16.         }
  17.     }
  18.        
  19.    
  20.     public static void main(String args[])//main method
  21.     {
  22.     Scanner in= new Scanner(System.in);
  23.     fibo fi=new fibo();
  24.     int lb,ub;
  25.     System.out.println("enter the lower and upper bound: ");
  26.     lb=in.nextInt();ub=in.nextInt();
  27.     System.out.println("the fibonacci numbers in the range "+lb+" - "+ub+" are:");
  28.     fi.fib(lb,ub);
  29.     }
  30. }// class ends
  31.  
RAW Paste Data