How to Check Given Number is Fibonacci or Not in Python?

19-Oct-2022

.

Admin

How to Check Given Number is Fibonacci or Not in Python?

Hi Dev,

Now, let's see post of How to Check Given Number is Fibonacci or Not in Python. I explained simply step by step Check if a given number is Fibonacci number in python. you will learn python - To find if a number is fibonacci or not. This post will give you simple example of fibonacci numbers in python. So, let's follow few step to create example of A Python Guide to the Fibonacci?.

See the following python program to check number is Fibonacci or not; as shown below:

let's see below simple example with output:

Example : How to Check Whether a Number is Fibonacci or Not


# python program to check if given

# number is a Fibonacci number

import math

# function to check perferct square

def checkPerfectSquare(n):

sqrt = int(math.sqrt(n))

if pow(sqrt, 2) == n:

return True

else:

return False

# function to check Fibonacci number

def isFibonacciNumber(n):

res1 = 5 * n * n + 4

res2 = 5 * n * n - 4

if checkPerfectSquare(res1) or checkPerfectSquare(res2):

return True

else:

return False

# main code

num = int(input("Enter an integer number: "))

# checking

if isFibonacciNumber(num):

print ("Yes,", num, "is a Fibonacci number")

else:

print ("No,", num, "is not a Fibonacci number")

Output:

Enter an integer number: 5

Yes, 5 is a Fibonacci number

I hope it can help you...

#Python