Sunday , 20 August 2017
Latest Posts
Home » Class 10 » Java Program to check if a number is in Fibonacci Series or not

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

#### Question:

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.

#### Programming Code:

```/**
* The class IsFibonacci inputs a number and checks whether
* it belongs to the Fibonacci Series or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class IsFibonacci
{
public static void main(String args[])throws IOException
{
System.out.print("Enter a number : "); // Inputting a number

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.

## [Question 1] ISC 2017 Computer Practical Paper Solved – Box Packing

Solution of Program 1 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input number of boxes to be packed and display the break-up of the cartons used in descending order of capacity

1. really crisp logic…thank you very much for this

2. Thank u so much..

3. Thank you sir this website is very helpful.

4. This is really an efficient site which is very helpful to learn basics of java…thnx sir.

5. Great …so far the only website in which error free programs are given……..thx sir:)

6. This is good it really helps people.in this program of fibonacci if 0 will be compare to c it will return false because at first time only c is becoming 1.please give me the answer.

7. Thank you . I find this website very helpful.

8. thank you sir, you are en extremely efficient teacher and guide! 😀