In this program user will enter a number and the program will check whether the number is in fibonacci series.
How to proceed on this program
After taking the number, an infinite loop will be executed to check the number in fibonacci series. The elements of the fibonacci series will also be generated within the loop. If the number is found, the loop will be terminated. Similarly if the number is found to be greater than any element of the series and at the same time it is less than the next element of the fibonacci series, it indicates that the number is not present in the fibonacci series. In this situation also the loop will be terminated.
import java.io.*;
class Find
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i, p=0,c=1,next,n;
public void take()throws Exception
{
System.out.println("Enter the number to search:");
n=Integer.parseInt(br.readLine());
while(true)
{
next=p+c;
if(n==c)
{
p=-1;
break;
}
if(n>c && n< next)
{
break;
}
p=c;
c=next;
}
if(p==-1)
System.out.println(n+ " lies in the fibonacci series");
else
System.out.println(n+ " is not in the fibonacci series");
}
}
No comments:
Post a Comment