Check if the number is Fibonacci

PROBLEM :

Check if a given number is Fibonacci number. Fibonacci number is a number which occurs in Fibonacci series.

Input:
The first line of input contains an integer T denoting the number of test cases.
Each test case contains a number.

Output:
Print "Yes" if the entered number is Fibonacci number else "No".

Constraints:
1<=t<=100
1<=n<=100

Example:

Input
2
34
41

Output
Yes
No

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Fibonacci(int ) ;
int main()
 {
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   if(Fibonacci(no))
       cout<<"Yes" ;
   else
       cout<<"No" ;
   cout<<endl ;
}
return 0;
}

int Fibonacci(int no)
{
    if(no==0)
        return 1 ;
    if(no==1)
        return 1 ;
       
    int a,b,c ;
    a=0 ;
    b=1 ;
   
    while(no>=c)
    {
        c=a+b ;
        a=b ;
        b=c ;
       
        if(no==c)
            return 1 ;
    }

    return 0 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )