Fibonacci Series

Print the first N terms of the Fibonacci sequence using a loop.

PythonBeginner
Python
# Program to print Fibonacci series up to N terms

n = int(input("Enter number of terms: "))

if n <= 0:
    print("Please enter a positive integer.")
else:
    a, b = 0, 1
    for _ in range(n):
        print(a)
        a, b = b, a + b

Output

Enter number of terms: 5
0
1
1
2
3

We maintain two variables (a, b) representing consecutive Fibonacci numbers and update them each iteration.