Fibonacci series program in python using recursive method

Posted by

In this tutorial we are going to learn how to print Fibonacci series in python program using recursion.

In this series number of elements of the series is depends upon the input of users. Program will print n number of elements in a series which is given by the user as a input.

Before moving directly on the writing Fibonacci series in python program, first you should know

What is Fibonacci Series?

A Fibonacci series is a series in which next number is a sum of previous two numbers.

For example : 0, 1, 1, 2, 3, 5, 8 ……

In Fibonacci Series, first number starts with 0 and second is with 1 and then its grow like,

0

1

0 + 1 = 1

1 + 1 = 2

1 + 2 = 3

2 + 3 = 5 and 

so on…

How our program will behave?

Here we have a function named fib() which will take a input and then return element. Each time it will call itself to calculate the elements of the series. 

Like if someone given 6 as a input then our program should return,

0, 1, 1, 2, 3, 5

Python program to print Fibonacci series using recursive methods

# Print Fibonacci series in python using Recursive method

# A Fibonacci series is a series in which next number is a sum of previous two numbers.
#input --> 5
#output--> 0 1 1 2 3

num=int(input("Enter number :- "))

print("Below is a fibonacci sequence...")

def fib(n):
    if n==0:
        return 0
    if n==1:
        return 1
    else:
        return fib(n-1)+fib(n-2)


for i in range(0,num):
    print(fib(i))

One comment

Leave a Reply

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