m2skills

fibocheck cpp

May 31st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. // PROGRAM TO CHECK IF A NUMBER IF A FIBONACCI NUMBER OR NOT
  2.  
  3. #include<iostream>
  4. #include<math.h>
  5. using namespace std;
  6.  
  7. // method to check if the number is a perfect squre or not
  8. bool isPerfect(int num){
  9.     int n = sqrt(num); 
  10.     if(n*n == num){
  11.         return true;
  12.     }else{
  13.         return false;
  14.     }
  15. }
  16.  
  17. // method to check if a number appears in fibonacci series or not
  18. bool isFibonacci(int num){
  19.  
  20.     // calculating numbers to check if they are perfect squares or not
  21.     int temp1 = 5*num*num - 4;
  22.     int temp2 = 5*num*num + 4;
  23.     if(isPerfect(temp1) || isPerfect(temp2)){
  24.         return true;
  25.     }else{
  26.         return false;
  27.     }  
  28. }
  29.  
  30. // main method
  31. int main(){
  32.     int myList[] = {22, 15, 5, 8, 21, 34, 38, 75, 55, 89};
  33.     for(int i=0; i<10; i++){
  34.         bool fibo = isFibonacci(myList[i]);
  35.         if(fibo){
  36.             cout<<myList[i]<<" is a Fibonacci number."<<endl;
  37.         }else{ 
  38.             cout<<myList[i]<<" is not a Fibonacci number."<<endl;
  39.         }  
  40.     }  
  41. }
Add Comment
Please, Sign In to add comment