C program to know whether a particular number present in the Fibonacci series or not.
Sample input1:
Number to be searched: 5
Output1:
Found
Sample input2:
Number to be searched: 7
Output1:
Not Found
Program:
#include<stdio.h>
int main()
{
int key;
int f0=0,f1=1,f2;
int a=0;
printf("\nNumber to be searched: ");
scanf("%d",&key);
if((f0==key)||(key==f1))
printf("\nFOUND");
else
{
f2=f0+f1;
while(f2<key)
{
f2=f0+f1;
if(f2==key)
a=1;
else{
f0=f1;
f1=f2;
}
}
if(a)
printf("\nFOUND");
else
printf("\nNot found");
}
return 0;
}
int main()
{
int key;
int f0=0,f1=1,f2;
int a=0;
printf("\nNumber to be searched: ");
scanf("%d",&key);
if((f0==key)||(key==f1))
printf("\nFOUND");
else
{
f2=f0+f1;
while(f2<key)
{
f2=f0+f1;
if(f2==key)
a=1;
else{
f0=f1;
f1=f2;
}
}
if(a)
printf("\nFOUND");
else
printf("\nNot found");
}
return 0;
}
No comments:
Post a Comment