This Python program reads an integer from the user and checks whether it is part of the Fibonacci sequence.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two.
# Read input number from user
n = int(input("Enter a number: "))
# Check if the number is in the Fibonacci sequence
a, b = 0, 1
found = False
while a <= n:
if a == n:
found = True
break
a, b = b, a + b
# Display result
if found:
print(f"{n} is a Fibonacci number.")
else:
print(f"{n} is not a Fibonacci number.")
Enter a number: 13
13 is a Fibonacci number.
Enter a number: 14
14 is not a Fibonacci number.
- The program starts with a = 0 and b = 1
- It generates Fibonacci numbers until it reaches or exceeds n
- If n matches any generated value, it is part of the sequence
- Otherwise, the loop ends and the program reports that n is not in the sequence