Program in Python to print Fibonacci sequence


Gaming Laptop Series

A Fibonacci sequence is consists of integer numbers whose first and second term is 0 and 1 respectively you can determine the coming terms by adding the previous two numbers of the sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 . . . . .

You can say that the nth term of the sequence is the sum of (n-1)th and (n-2) th term.

In this article, we will use recursion to write our program in Python to print the Fibonacci sequence.

What is recursion?

The recursion occurs when a thing is defined in terms of itself. In programming, a function which calls itself either directly or indirectly is called a recursive function. Using recursion some problems can be solved very easily.

Here we will use recursion to write our Python program that will print Fibonacci series.

Source code of Python program to print Fibonacci sequence

You can execute the following program to print Fibonacci sequence.

def fibonacci_seq(n):
    if(n == 0):
      return 0
    elif(n == 1):
      return1
    else:
     return (fibonacci_seq(n - 2) + fibonacci_seq(n - 1))
n = int(input("Enter number of terms you want to print:  "))
print("Fibonacci Series:", end = ' ')
for n in range(0, n):
    print(fibonacci_seq(n), end = ' ')

Now if you execute this program it will print the Fibonacci sequence up to the number of terms you entered.

There are different ways in which you can write this program. In the above program, we used recursion to print Fibonacci sequence.

Now if you have a query then write use in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.