Python program to check whether a number is Fibonacci or not
import math
def isPerfectSquare(num):
square_root = int(math.sqrt(num))
return square_root * square_root == num
def isFibonacciNumber(n):
# A number is a Fibonacci number if one of the conditions is true
if isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4):
return True
else:
return False
# Example usage:
number_to_check = int(input("Enter a number to check if it's a Fibonacci number: "))
if isFibonacciNumber(number_to_check):
print(f"{number_to_check} is a Fibonacci number.")
else:
print(f"{number_to_check} is not a Fibonacci number.")
Output 1:
Enter a number to check if it's a Fibonacci number: 12
12 is not a Fibonacci number.
Output 2:
Enter a number to check if it's a Fibonacci number: 21
21 is a Fibonacci number.