Fibonacci program using recursion in python

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms.This means to say the nth term is the sum of (n-1)th and (n-2)th term.


Source Code

# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))

Output

Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

Note: To test the program, change the value of nterms.

In this program, we store the number of terms to be displayed in nterms.

A recursive function recur_fibo() is used to calculate the nth term of the sequence. We use a for loop to iterate and calculate each term recursively.

Visit here to know more about recursion in Python.

Fibonacci program using recursion in python

Fibonacci program using recursion in python

Python Tutorial+

Python Basic Programs

Python Data Program

Python Condition Loops

Python Functions

Python Program to Display Fibonacci Sequence Using Recursion

Fibonacci sequence:

A Fibonacci sequence is a sequence of integers which first two terms are 0 and 1 and all other terms of the sequence are obtained by adding their preceding two numbers.

For example: 0, 1, 1, 2, 3, 5, 8, 13 and so on...

See this example:

Output:

Fibonacci program using recursion in python


Fibonacci program using recursion in python
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

Fibonacci program using recursion in python
Fibonacci program using recursion in python
Fibonacci program using recursion in python






This is a Python Program to find the fibonacci series using recursion.

Problem Description

The program takes the number of terms and determines the fibonacci series using recursion upto that term.

Problem Solution

1. Take the number of terms from the user and store it in a variable.
2. Pass the number as an argument to a recursive function named fibonacci.
3. Define the base condition as the number to be lesser than or equal to 1.
4. Otherwise call the function recursively with the argument as the number minus 1 added to the function called recursively with the argument as the number minus 2.
5. Use a for loop and print the returned value which is the fibonacci series.
6. Exit.

Program/Source Code

Here is source code of the Python Program to find the fibonacci series using recursion. The program output is also shown below.

def fibonacci(n):
    if(n <= 1):
        return n
    else:
        return(fibonacci(n-1) + fibonacci(n-2))
n = int(input("Enter number of terms:"))
print("Fibonacci sequence:")
for i in range(n):
    print(fibonacci(i))

Program Explanation

1. User must enter the number of terms and store it in a variable.
2. The number is passed as an argument to a recursive function.
3. The base condition is that the number has to be lesser than or equal to 1.
4. Otherwise the function is called recursively with the argument as the number minus 1 added to the function called recursively with the argument as the number minus 2.
5. The result is returned and a for statement is used to print the fibonacci series.

Runtime Test Cases

 
Case 1:
Enter number of terms:5
Fibonacci sequence:
0 1 1 2 3
 
Case 2:
Enter number of terms:7
Fibonacci sequence:
0 1 1 2 3 5 8

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Fibonacci program using recursion in python

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

How do you write Fibonacci series in python recursion?

Python Program to Display Fibonacci Sequence Using Recursion.
def recur_fibo(n):.
if n <= 1:.
return n..
return(recur_fibo(n-1) + recur_fibo(n-2)).
# take input from the user..
nterms = int(input("How many terms? ")).
# check if the number of terms is valid..

What is Fibonacci series using recursion?

Fibonacci Series Using Recursion in C refers to a number series. The Fibonacci series is created by adding the preceding two numbers ahead in the series. Zero and one are the first two numbers in a Fibonacci series. We generate the rest of the numbers by adding both the previous numbers in the series.

How do you solve Fibonacci in Python?

Source Code We initialize the first term to 0 and the second term to 1. If the number of terms is more than 2, we use a while loop to find the next term in the sequence by adding the preceding two terms. We then interchange the variables (update it) and continue on with the process.

What is Fibonacci series in Python?

The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation. Fn = Fn-1 + Fn-2.