Python Program to Fibonacci Series

Write a Python Program to Fibonacci Series

Python Programming examples interviewgigFibonacci Series in Mathematics:

In mathematics, the Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. So, the Fibonacci series begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

Python Program to Generate Fibonacci Series:

Here’s a Python program that generates the Fibonacci series up to a specified number of terms:

n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Series:")
for _ in range(n):
    print(a, end=" ")
    a, b = b, a + b

Explanation:

  • The program starts by taking an integer input from the user using the input() function and converting it into an integer using the int() function. This input is stored in the variable n.
  • Two variables, a and b, are initialized to 0 and 1, respectively. These will be used to generate the Fibonacci series.
  • The program uses a for loop that iterates n times to generate the Fibonacci series.
  • Inside the loop, the current value of a is printed using the print() function with the end argument set to an empty space to avoid printing a newline.
  • Then, the values of a and b are updated such that a becomes b, and b becomes the sum of the previous values of a and b.
  • The loop continues until n terms of the Fibonacci series have been printed.

Output:

Enter the number of terms: 6
Fibonacci Series:
0 1 1 2 3 5

In this example, if the user enters 6, the program generates the first 6 terms of the Fibonacci series and displays them as output: 0, 1, 1, 2, 3, and 5.

Scroll to Top