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

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    

Source Code :

Coding Ninjas Assignment Solutions
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


#include<iostream>

using namespace std;

bool checkMember(int n){

   int  c,first = 0, second = 1, next,f,t=0;

   cin >> n;

   for ( c = 0 ; c < 50 ; c++ )

   {

      if ( c <= 1 )

         next = c;

      else

      {

         next = first + second;

         first = second;

         second = next;

      }

       if(next==n){

           return 1;

           break;

       }

   }

if(t==0)

    return 0;

}


Post a Comment

0 Comments