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…
What is Recursion?
Recursion is a programming technique where a function calls itself directly or indirectly in order to solve a problem. It breaks down a large problem into smaller instances of the same problem, each of which is easier to manage and solve. Recursion is typically used in cases where a problem can naturally be divided into similar subproblems.
Characteristics of Recursion
- Base Case: This is the condition under which the recursive function will stop calling itself. Without a base case, the function would call itself indefinitely, leading to a stack overflow.
- Recursive Case: This part of the function is where it calls itself to solve a smaller part of the problem.
Example of Recursion: Calculating Factorial
The factorial of a number (denoted as 𝑛!n!) is a classic example of a recursive problem. Factorial of a number 𝑛n is the product of all positive integers less than or equal to 𝑛n. Mathematically, it’s defined as: 𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×1
For example, 5!=5×4×3×2×1=120
How our program will behave?
Its Logic is different from Fibonacci series program in c using iterative method.
Here we have a function named fibonacci() 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
n = int(input("please give a number for fibonacci series : "))
first,second=0,1
def fibonacci(num):
if num == 0:
return 0
elif num == 1:
return 1
else:
return fibonacci(num-1)+fibonacci(num-2)
print("fibonacci series are : ")
for i in range(0,n):
print(fibonacci(i))
Output:
please give a number for fibonacci series : 6
fibonacci series are :
0
1
1
2
3
5
Program Explanation
- User Input: The program begins by asking the user to enter the number 𝑛n, which determines how many terms of the Fibonacci sequence will be printed.
- Initialization: Two variables,
first
andsecond
, are initialized to 0 and 1. These are the first two terms of the Fibonacci sequence, although they are not directly used in the recursive function. - Recursive Function: A function named
fibonacci
is defined to compute Fibonacci numbers:- It checks if the input
num
is 0, returning 0 (base case). - It checks if
num
is 1, returning 1 (second base case). - For all other values, it returns the sum of the Fibonacci number at positions
num-1
andnum-2
, effectively calling itself twice for each call (recursive case).
- It checks if the input
- Print Sequence: The program prints a header “fibonacci series are : ” and then iterates from 0 to 𝑛−1 , calling the
fibonacci
function for each value and printing the result. This prints each term of the Fibonacci sequence from the first to the nth term.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…