Python program to print the Fibonacci sequence

The Fibonacci sequence is the series of number: 0,,1,1,2,3,5,….. The next number is found by adding up the tow numbers before it.

How to

  • The 2 is found by adding the two numbers before it (1+1)
  • The 3 is found by adding the two numbers before it (1+2)

Program

n=int(input("Enter number:  "))
def fib(n):
    a=0
    b=1
    for i in range(n):
        if i<=1:
            n=i
        else:
            n=a+b
            a=b
            b=n
            print(n)
fib(n)

Output

Enter number 5
0,1,1,2,3

Leave a Reply