Fibonacci Series Membership Program in Java and Python
Fibonacci Series membership program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program 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, ...
Algorithm:
Step 1: Start.
Step 2: Accept the number n.
Step 3: If n is negative, display an error message and stop.
Step 4: Initialize first Fibonacci terms a = 0 and b = 1.
Step 5: Initialize a flag to record whether n is found.
Step 6: Repeat while the current Fibonacci term is less than or equal to n.
Step 7: If the current term is equal to n, set the flag and stop the loop.
Step 8: Otherwise calculate next = a + b.
Step 9: Shift a to b and b to next for the next cycle.
Step 10: Display whether n is present in the Fibonacci series according to the flag.
Step 11: Stop.
Explanation:
The program generates Fibonacci terms until it either reaches the given number or crosses it. This avoids needing any advanced formula and keeps the logic easy to trace.
Two variables store consecutive Fibonacci terms. The next term is obtained by adding these two values, then the variables are shifted forward.
If any generated term becomes equal to the input number, the number belongs to the Fibonacci series. A flag is used to remember this result.
If the generated term becomes greater than the input before a match is found, the search stops because all later Fibonacci terms will be even larger.
The update order matters: the next term is calculated before shifting the two previous terms. This preserves the pair of consecutive Fibonacci values needed for the next loop cycle.
The flag variable separates calculation from final output. The loop may stop because the number is found or because the generated term crosses the input, and the flag records which condition occurred.
The program checks membership in the Fibonacci sequence by generating terms until the current term reaches or crosses the given number. Starting with 0 and 1, each next term is the sum of the previous two terms. If a generated term equals the input, the number belongs to the series. If the generated term becomes greater than the input, the number cannot appear later because Fibonacci terms keep increasing. This stopping condition avoids unnecessary generation beyond the required point.
Java Program:
/**
* The class IsFibonacci inputs a number and checks whether
* it belongs to the Fibonacci Series or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class IsFibonacci
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : "); // Inputting a number
int n = sc.nextInt();
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.");
}
}
}Equivalent Python Program:
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.
n = int(input("Enter a number: "))
if n < 0:
print("Kindly enter a positive number.")
else:
a = 0
b = 1
c = 0
while c < n:
c = a + b
a = b
b = c
if c == n or n == 0:
print("The number belongs to Fibonacci Series.")
else:
print("The number does not belong to Fibonacci Series.")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.