0
454views
Write a python program for Fibonacci sequence
1 Answer
0
7views

* Fibonacci Sequence is*

0 1 1 2 3 5 8 13 21 34 55 89

Explanation

Iteration 1 :- a=0 b=1 c=a + b

  • 3rd no(c) = 1st(a) + 2nd(b)

    3rd no(c) = 0 +1 = 1

Iteration 2 :- a=1 b=1 c=a + b

  • 4th no(c) = 2nd(a) + 3rd(b)

    4th no = 1 +1 = 2

    Sequence would be 0 1 1 2

Iteration 3 :- a=1 b=2 c=a + b

  • 5th no = 3rd + 4th

    5th no = 1 +2 = 3

    Sequence would be 0 1 1 2 3

Code

a=0
b=1
n=int(input("Enter the no of elements in the Fibonacci Series:- "))
if n<0:
    print("Enter the valid No")
elif n==0:
     print(a)
elif n==1:
    print(b)
else:
    for i in range (n):
        c=a+b
        print(c,end=' ')
        a=b
        b=c

Output:-

enter image description here

Please log in to add an answer.