Fibonacci Series - Learn Engineering

All the news and updates relevant to Engineering. You can also Write a post here

Breaking

Friday, June 19, 2020

Fibonacci Series

python-mini

Question

To Print the Fibonacci Series.

Sample TestCaseExpected Output

How many numbers? 5                                                                             Fibonacci Series 

                                                                                                                                    0
                                                                                                                                    1
                                                                                                                                    1
                                                                                                                                    2
                                                                                                                                    3                                                 





Answer


nterms=int(input("How many terms ? "))
n1,n2=0,1
count=0
if nterms<=0:
    print("Enter a Positive Integer")
elif nterms==1:
    print("Fibonacci Series")
    print(n1)
else:
    print("Fibonacci Series")
    while count<nterms:
        print(n1)
        nth=n1+n2
        n1=n2
        n2=nth
        count+=1   

No comments:

Post a Comment