Python Program to print Fibonacci Sequence up to given range.


Each number in the sequence is the sum of the two numbers that precede it. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 and so on.


Python Code:

  • Python Program to print Fibonacci Sequence up to given range.

ft = 0
sd = 1
print(ft,sd,end=" ")
for i in range(2,20):    # Starting from range "2" because two numbers- 
                         # are already print above.
    td = ft+sd
    print(td,end=" ")
    ft = sd
    sd = td

Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181


  • Python Program to check a given number is belongs to Fibonacci series or not.

num=int(input("Enter a number:"))
ft = 0
sd = 1
for i in range(0,num):
    td = ft+sd
    ft = sd
    sd = td
    if num == td or num == 0:
        print("The given number belongs to Fibonacci series.")
        break
else:
    print("The given number do not belongs to Fibonacci series.")

Output:

Enter a number: 152

The given number do not belongs to Fibonacci series.

Enter a number: 233

The given number belongs to Fibonacci series.



Recent Posts

See All