Fibonacci Series Generation

Write a Python program that prints the Fibonacci series up to n terms. The integer n is input by the user.

Example 1:

Input: 5 

Output: 0, 1, 1, 2, 3

Example 2:

Input: 10 

Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

In the Fibonacci series, the next number is the sum of the previous two numbers. You can start by initializing two variables to 0 and 1.

def print_fibonacci(n):
    a, b = 0, 1
    for i in range(n):
        print(a, end=' ')
        a, b = b, a + b

print_fibonacci(5)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!