Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PROGRAM TO CHECK IF A NUMBER IS A FIBOANCCI NUMBER OR NOT
- import java.util.Scanner;
- import java.io.*;
- public class fibonacci{
- public static void main(String a[]){
- int[] myList = {22, 15, 5, 8, 21, 34, 38, 75, 55, 89};
- for(int i=0; i<myList.length; i++){
- boolean fibo = isFibonacci(myList[i]);
- if(fibo){
- System.out.println(myList[i] + " is a Fibonacci number.");
- }else{
- System.out.println(myList[i] + " is not a Fibonacci number.");
- }
- }
- }
- // method to check if the number is a perfect squre or not
- static boolean isPerfect(int num){
- int n = (int)Math.sqrt(num);
- if(n*n == num){
- return true;
- }else{
- return false;
- }
- }
- // method to check if a number appears in fibonacci series or not
- static boolean isFibonacci(int num){
- // calculating numbers to check if they are perfect squares or not
- int temp1 = 5*num*num - 4;
- int temp2 = 5*num*num + 4;
- if(isPerfect(temp1) || isPerfect(temp2)){
- return true;
- }else{
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment