Wednesday, November 4, 2009

Check whether given number is Fibonacci OR not

Algo : A given number is said to be fibonacci, if 5*N*N -4 or 5*N*N +4 is a square

#include
int isSquare(int num);
main()
{
int num, temp;
printf("Enter the number\n");
scanf("%d", &num);

temp = 5 *(num * num) - 4;

if(isSquare(temp) || isSquare(temp+8)){
printf("It is finonacci number\n");
}else {
printf("It is NOT finonacci number\n");
}
}
int isSquare(int num)
{
int i=1;
while(num>0)
{
num = num - i;
i = i+2;
}
if(num==0)
return 1;
else
return 0;
}

1 comment:

Sara Reid said...

Yes, I have cheeked it. It is Fibonacci series program. Enter 5, you got output as :

0 1 1 2 3 5..

dhea