Friday, 17 July 2015

To Check A Number Is Fibonacci Number

If you want to know what is a fibonacci number
visit  


And the C code for it is




Now how to check if a number is fibonacci number.Using math it becomes simple!!


If n is a number then Either 5n^2+4 or 5n^2-4 or both is(are) perfect square then the number is a Fibonacci number


perfect square:  9 = 3*3 ; 16 = 4*4;


If you want the proof for it Click here and Goto 418 page


The C code for it is


#include<stdio.h>
#include<math.h>

int ispsquare(int num)
{
   int s=sqrt(num);
   
   if(s*s==num)
      return 1;
    else
       return 0;
}
void fibonacci(int num)
{
   
    int check1 = 5*num*num+4;
    int check2 = 5*num*num-4;
    
    if(ispsquare(check1)||ispsquare(check2))
        printf("Its's a Fibonacci number");
    else
        printf("Its's not a Fibonacci number");
}
main(){
   
   int num;
   
   printf("Enter a number to check if it is a fibonacci number : ");
   scanf("%d",&num);
   
   fibonacci(num);
}



No comments:

Post a Comment