Fibonacci Number

 Fibonacci Number

Send Feedback

Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false.

Fibonacci Series is defined by the recurrence

    F(n) = F(n-1) + F(n-2)

where F(0) = 0 and F(1) = 1


Input Format :
Integer N
Output Format :
true or false
Constraints :
0 <= n <= 10^4
Sample Input 1 :
5
Sample Output 1 :
true
Sample Input 2 :
14
Sample Output 2 :
false    



bool checkMember(int n){

/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
// int a=0;
// int b=1;
// int c;
// int start=0;
// while(start<n){
// c=a+b;
// a=b;
// b=c;
// if(c==n){
// return true;
// }
// start++;
// }
// return false;
int f1=0,f2=1,fib;
//cin>>n;
for(int i=0;i<n;i++){
fib=f1+f2;
f1=f2;
f2=fib;
}
return fib==n;

}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas