Easy Tutorial
❮ Python Func Number Log10 Python Func Chr Html ❯

Python Using Recursive Fibonacci Sequence

Python3 Examples

The following code uses recursion to generate the Fibonacci sequence:

Example (Python 3.0+)

# Filename : test.py
# author by : www.tutorialpro.org

def recur_fibo(n):
   """Recursive function
   Outputs the Fibonacci sequence"""
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

# Get user input
nterms = int(input("How many terms do you want to display? "))

# Check if the input number is correct
if nterms <= 0:
   print("Please enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))

Executing the above code produces the following result:

How many terms do you want to display? 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

Python3 Examples

❮ Python Func Number Log10 Python Func Chr Html ❯