Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Find fibonacci series upto n using lambda
Next article icon

Python Program to Display Fibonacci Sequence Using Recursion

Last Updated : 19 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a task to write the Fibonacci sequence using recursion. we will take the range as input of integer and then print the Fibonacci Sequence. In this article, we will see the method of Python Program to Display Fibonacci Sequence Using Recursion.

Example:

Input: n = 9
Output: 0 1 1 2 3 5 8 13 21
Explanation: Here, we have n = 9, and we print the
Fibonacci Sequence by adding one element with its
subsequent element.

What is the Fibonacci Sequence?

The Fibonacci sequence is like a mathematical melody, where each number dances to the rhythm of the two preceding ones. It usually begins its graceful dance with the numbers 0 and 1. This sequence is not just a mere mathematical curiosity; it's like a versatile performer that appears on the stages of various mathematical and computer science domains. The Fibonacci sequence is defined by the recurrence relation:

F(n) = F(n-1) + F(n-2)

where ( 0 ) = 0 F(0)=0 and ( 1 ) = 1 F(1)=1.

Python Program to Display Fibonacci Sequence Using Recursion

Below, are the implementation of Python Program to Display Fibonacci Sequence Using Recursion.

  • The code defines a recursive function, fib, to generate Fibonacci series.
  • It also contains a function print_fib to handle edge cases and initiate the Fibonacci series printing.
  • The program checks for invalid inputs and prints appropriate messages.
  • It initializes the series with 0 and 1 and calculates subsequent terms by summing the previous two.
  • The driver code calls print_fib with the desired number of Fibonacci series terms, which is set to 9 in this case.

Implementation

Python3
# Recursive function to print the Fibonacci series def fib(n, prev1, prev2):     if n < 3:         return     fn = prev1 + prev2     prev2 = prev1     prev1 = fn     print(fn, end=" ")     fib(n - 1, prev1, prev2)  def print_fib(n):     # When the number of terms is less than 1     if n < 1:         print("Invalid number of terms")     elif n == 1:         print(0)     elif n == 2:         print("0 1")     else:         print("0 1", end=" ")         fib(n, 1, 0)  # Driver code if __name__ == "__main__":     n = 9     # Function call     print_fib(n) 

Output
0 1 1 2 3 5 8 13 21 

Time complexity: O(n),
Auxiliary Space: O(n),


Next Article
Python | Find fibonacci series upto n using lambda

K

kumarwatsal43
Improve
Article Tags :
  • Python
  • Python Programs
  • DSA
  • Python-DSA
Practice Tags :
  • python

Similar Reads

  • Python program to find the power of a number using recursion
    Given a number N and power P, the task is to find the power of a number ( i.e. NP ) using recursion. Examples: Input: N = 2 , P = 3Output: 8 Input: N = 5 , P = 2Output: 25 Approach: Below is the idea to solve the above problem: The idea is to calculate power of a number 'N' is to multiply that numbe
    3 min read
  • Python program to find the factorial of a number using recursion
    A factorial is positive integer n, and denoted by n!. Then the product of all positive integers less than or equal to n. [Tex]n! = n*(n-1)*(n-2)*(n-3)*....*1 [/Tex] For example: [Tex]5! = 5*4*3*2*1 = 120 [/Tex] In this article, we are going to calculate the factorial of a number using recursion. Exa
    1 min read
  • Python Program to Find the Total Sum of a Nested List Using Recursion
    A nested list is given. The task is to print the sum of this list using recursion. A nested list is a list whose elements can also be a list. Examples : Input: [1,2,[3]] Output: 6 Input: [[4,5],[7,8,[20]],100] Output: 144 Input: [[1,2,3],[4,[5,6]],7] Output: 28 Recursion: In recursion, a function ca
    5 min read
  • Python | Find fibonacci series upto n using lambda
    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 with seed values F0 = 0 and F1 = 1. Find the series of fi
    2 min read
  • Fibonacci Series Program In Python Using Iterative Method
    Fibonacci series is a series where each number is the sum of its two previous numbers. In this article, we are going to generate Fibonacci series in Python using Iterative methods. We will be covering both the loops i.e. for loop and while loop. In this article, we will be covering all the concepts
    4 min read
  • Fibonacci Series In Python Using For Loop'
    The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. This series is not only intriguing due to its inherent mathematical properties but also widely
    3 min read
  • Python Program for Print Number series without using any loop
    Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop. Examples : Input : N = 15 K = 5 Output :
    3 min read
  • Python Program to Count ways to reach the n'th stair
    There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top. Consider the example shown in diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is
    3 min read
  • Python program to print Aitken's array
    Given a number n. The task is to print the Aikten's array upto n. Examples: Input: 5 Output: [1] [1, 2] [2, 3, 5] [5, 7, 10, 15] [15, 20, 27, 37, 52] Input: 7 Output: [1] [1, 2] [2, 3, 5] [5, 7, 10, 15] [15, 20, 27, 37, 52] [52, 67, 87, 114, 151, 203] [203, 255, 322, 409, 523, 674, 877] To print it
    2 min read
  • Reverse sequence of strictly increasing integers in a list-Python
    The task of reversing the sequence of strictly increasing integers in a list in Python involves identifying consecutive increasing subsequences and reversing each subsequence individually. For example, given a list a = [0, 1, 9, 8, 7, 5, 3, 14], the goal is to reverse the strictly increasing subsequ
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences