Python Program to Find nth Term of Fibonacci Series Using Recursive Function

In mathematics, Fibonacci terms are generated recursively as:

                 0          if n=1
                 
fib(n) =         1          if n=2

         fib(n-1)+fib(n-2)  otherwise

First few terms of Fibonacci series are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...


This Python program calculates nth term of Fibonacci series using recursive function.


Python Source Code


# nth term of Fibonacci series using recursion
def fib(n):
    if n==1:
        return 0
    elif n==2:
        return 1
    else:
        return fib(n-1) + fib(n-2)

# Reading number of terms
term = int(input("Which term of Fibonacci series? "))

result = fib(term)
print("\n%dth term of Fibonacci series is: %d" %(term, result))

Output

Which term of Fibonacci series? 10

10th term of Fibonacci series is: 34