Java Program to check if a number is in Fibonacci Series or not

Write a program in Java to accept a number and check whether it belongs to the Fibonacci Series (sequence) or not.
Fibonacci Series:
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
The first two numbers in the series is ‘0’ and ‘1’ and every next number is found by adding up the two numbers before it.
The 2 is found by adding the two numbers before it (1+1)
Similarly, the 3 is found by adding the two numbers before it (1+2),
And the 5 is (2+3),
and so on!
Example: the next number in the sequence above would be 21+34 = 55
It is that simple!
Here is a longer list:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, …

How to Proceed:

Whenever you are told to check whether any number belongs to any particular series or not use this simple logic:
1. Run a while loop which generates numbers from that series and it goes on till they are less than the given number (which we want to check)
Example: If you are told to check whether 8 belongs to the Fibonacci series or not, then run a while loop which will run till the last generated term in the Fibonacci series is less than 8.
So, the terms generated will be:
0, which is less than 8, so the loop will continue.
1, which is less than 8, so the loop will continue.
1, which is less than 8, so the loop will continue.
2, which is less than 8, so the loop will continue.
3, which is less than 8, so the loop will continue.
5, which is less than 8, so the loop will continue.
8, which is NOT less than 8, so the loop will STOP.
2. So, the while loop will terminate if the last number generated from the series is either ‘EQUAL TO’ or ‘GREATER THAN’ the given number.
3. Now, just check if the last generated number is EQUAL TO the given number or not. If yes, then it belongs to that series, otherwise not.
As in the above example, since 8 is EQUAL TO 8, it belongs to the Fibonacci Series.
Similarly, if we are told to check whether 10 belongs to the Fibonacci series or not, then the terms generated will be:
0 which is less than 10, so the loop will continue.
1 which is less than 10, so the loop will continue.
1 which is less than 10, so the loop will continue.
2 which is less than 10, so the loop will continue.
3 which is less than 10, so the loop will continue.
5 which is less than 10, so the loop will continue.
8 which is less than 10, so the loop will continue.
13 which is NOT less than 10, so the loop will STOP.
Since 13 is NOT EQUAL TO 10, it does NOT belong to the Fibonacci Series.

import java.io.*;
class IsFibonacci
{
public static void main(String args[])throws IOException
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter a number : "); // Inputting a number
     int n = Integer.parseInt(br.readLine());
      
     if(n<0)
        System.out.println("Kindly enter a positive number.");
     else
     {
         int a=0, b=1 ,c=0;
         /* 'a' is the 1st term, 'b' is the 2nd term and 'c' is the 3rd term
          * 'c' stores the last generated term of the Fibonacci series */
      
          while(c<n) // Loop goes on till the 3rd term is less than the given number
          {
              c = a + b; // Generating the terms of Fibonacci Series
              a = b;
              b = c;
          }
      
          /* When the control comes out of the while loop, either the
           * 3rd term is equal to the number or greater than it */
         
           if(c==n) // If the last term = number, then it belongs to Fibonacci Series
              System.out.println("Output : The number belongs to Fibonacci Series.");
           else
              System.out.println("Output : The number does not belong to Fibonacci Series.");
     }
   }
}



Output:

1. Enter a number : 377
Output : The number belongs to Fibonacci Series.
2. Enter a 2 digit number : 100
Output : The number does not belong to Fibonacci Series.

No comments:

Post a Comment