Program to print the Fibonacci sequence up to n terms

# Fibonacci Sequence
n_terms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if n_terms <= 0:
    print("Please enter a positive integer")
elif n_terms == 1:
    print("Fibonacci sequence upto", n_terms, ":")
    print(n1)
else:
    print("Fibonacci sequence:")
    while count < n_terms:
        print(n1)
        nth = n1 + n2
        n1 = n2
        n2 = nth
        count += 1
Output:
How many terms? 5
Fibonacci sequence:
0
1
1
2
3

Leave a Reply

Your email address will not be published. Required fields are marked *