Q:

Python program to check whether a given number is a Fibonacci number or not

belongs to collection: Python basic programs

0

Python program to check whether a given number is a Fibonacci number or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python program to check Fibonacci number

# 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

First run:
Enter an integer number: 13
Yes, 13 is a Fibonacci number

Second run:
Enter an integer number: 144
Yes, 144 is a Fibonacci number

Third run:
Enter an integer number: 143
No, 143 is not a Fibonacci number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to find power of a number using exp... >>
<< Create a function to return the absolute the given...