Python program to generate first n Fibonacci numbers

Python program to print first n Fibonacci numbers

Write a Python program to accept a number from the user, and generate first n Fibonacci numbers.

Video Tutorial

What is the meaning of a Fibonacci series ..?

Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13 and so on…

The first number in the Fibonacci series is 0 (say f1) and the second number is 1 (say f2).

The next Fibonacci numbers are calculated as follows,

Repeat the following steps for n-2 times

f3 = f1 + f2

f1 = f2

f2 = f3

Source Code to print all Prime Numbers in the given range Program in Python

n = int (input("Enter the number of terms needed in the Fibonacci series: "))

if (n<0):
    print ("Enter a positive number")
else:
    f1, f2 = 0, 1
    if n == 1:
        print (f1)
    elif n == 2:
        print (f1,f2)
    else:
        print (f1,f2, end = ' ')
        for i in range (3, n+1):
            f3 = f1 + f2
            print (f3, end = ' ')
            f1 = f2
            f2 = f3

Output:

Case 1:

See also  Python program extract decimal number in the range of 00-99

Enter the number of terms needed in the Fibonacci series: -10

Enter a positive number

Case 2:

Enter the number of terms needed in the Fibonacci series: 20

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Summary:

This tutorial discusses how to write a Python program to generate first n Fibonacci numbers. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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