m2skills

fibocheck java

May 31st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. // PROGRAM TO CHECK IF A NUMBER IS A FIBOANCCI NUMBER OR NOT
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5.  
  6. public class fibonacci{
  7.     public static void main(String a[]){
  8.         int[] myList = {22, 15, 5, 8, 21, 34, 38, 75, 55, 89};
  9.         for(int i=0; i<myList.length; i++){
  10.             boolean fibo = isFibonacci(myList[i]);
  11.             if(fibo){
  12.                 System.out.println(myList[i] + " is a Fibonacci number.");
  13.             }else{ 
  14.                 System.out.println(myList[i] + " is not a Fibonacci number.");
  15.             }  
  16.         }  
  17.     }
  18.    
  19.     // method to check if the number is a perfect squre or not
  20.     static boolean isPerfect(int num){
  21.         int n = (int)Math.sqrt(num);
  22.        
  23.         if(n*n == num){
  24.             return true;
  25.         }else{
  26.             return false;
  27.         }
  28.     }
  29.    
  30.     // method to check if a number appears in fibonacci series or not
  31.     static boolean isFibonacci(int num){
  32.     // calculating numbers to check if they are perfect squares or not 
  33.         int temp1 = 5*num*num - 4;
  34.         int temp2 = 5*num*num + 4;
  35.         if(isPerfect(temp1) || isPerfect(temp2)){
  36.             return true;
  37.         }else{
  38.             return false;
  39.         }  
  40.     }
  41. }
Add Comment
Please, Sign In to add comment