Check if a given number is Fibonacci number in Python
Hello guys, now we are going to check if a given number is Fibonacci number in Python. If you are here, then you are moving in the right direction looking for the solution. Let me first explain to you guys what is a Fibonacci number.
I know you are here just because you are in need of this awesome trick to check if a given number is a Fibonacci number in Python by checking if the existence of that number in the Fibonacci sequence.
Generally, a Fibonacci sequence starts with 0 and 1 following 0. Then immediately the next number is going to be the sum of its two previous numbers. For example, the 3rd number in the Fibonacci sequence is going to be 1. Because its previous two numbers were 0 and 1. so, the sum of those numbers is 1.
In the same way, we are going to check for any number if it is a Fibonacci number.
Check for any number if it is a Fibonacci in Python:
n=int(input("Enter the number: ")) c=0 a=1 b=1 if n==0 or n==1: print("Yes") else: while c<n: c=a+b b=a a=c if c==n: print("Yes") else: print("No")
Enter the number: 34
Enter the number: 10
If the input is 0 or 1 then it is the Fibonacci number and prints yes. Else the integer input will be compared with c(a variable in the code) because c is a Fibonacci number and it goes on by adding with its previous number and stops when c is not less than the integer input.
If c is equal to n then it prints yes else it prints no. so guys, this is the code and its explanation. Hope you enjoyed it.