Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PROGRAM TO CHECK IF A NUMBER IF A FIBONACCI NUMBER OR NOT
- #include<iostream>
- #include<math.h>
- using namespace std;
- // method to check if the number is a perfect squre or not
- bool isPerfect(int num){
- int n = sqrt(num);
- if(n*n == num){
- return true;
- }else{
- return false;
- }
- }
- // method to check if a number appears in fibonacci series or not
- bool 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;
- }
- }
- // main method
- int main(){
- int myList[] = {22, 15, 5, 8, 21, 34, 38, 75, 55, 89};
- for(int i=0; i<10; i++){
- bool fibo = isFibonacci(myList[i]);
- if(fibo){
- cout<<myList[i]<<" is a Fibonacci number."<<endl;
- }else{
- cout<<myList[i]<<" is not a Fibonacci number."<<endl;
- }
- }
- }
Add Comment
Please, Sign In to add comment