Thursday, 30 July 2026

πŸš€ Day 94/150 – Recursive Fibonacci in Python

 

πŸš€ Day 94/150 – Recursive Fibonacci in Python

The Fibonacci sequence is one of the most popular examples used to understand recursion. In this sequence, each number is the sum of the two preceding numbers.

Fibonacci Sequence:



0, 1, 1, 2, 3, 5, 8, 13, 21, ...

In this post, we'll explore four different ways to generate Fibonacci numbers using recursion and related approaches.


Method 1 – Basic Recursive Function

A recursive function calls itself to calculate the Fibonacci number.

def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(6))








Output
8

Explanation
    If n is 0 or 1, the function returns n.
  • Otherwise, it returns the sum of the previous two Fibonacci numbers.
  • The function keeps calling itself until it reaches the base case.

Method 2 – Taking User Input

Calculate the Fibonacci number recursively using user input.


def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) num = int(input("Enter the position: ")) print("Fibonacci Number:", fibonacci(num))










Sample Input
7

Output

Fibonacci Number: 13

Explanation

  • The user enters the position in the Fibonacci sequence.
  • The recursive function calculates the Fibonacci number at that position.
  • The result is displayed.

Method 3 – Print Fibonacci Series Using Recursion

Print the first n Fibonacci numbers recursively.
def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) terms = 7 for i in range(terms): print(fibonacci(i), end=" ")












Output
0 1 1 2 3 5 8

Explanation

  • The for loop calls the recursive function for each position.
  • Each Fibonacci number is printed in sequence.
  • This generates the first terms Fibonacci numbers.

Method 4 – Recursive Function with Error Handling

Handle invalid input such as negative numbers.

def fibonacci(n): if n < 0: return "Position cannot be negative." if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(-5))












Output
Position cannot be negative.

Explanation

  • The function first checks whether the position is negative.
  • If it is, an error message is returned.
  • Otherwise, recursion proceeds normally.

Comparison of Methods

MethodBest For
Basic RecursionLearning recursion
User InputInteractive programs
Recursive SeriesPrinting multiple Fibonacci numbers
Error HandlingValidating user input

πŸ”₯ Key Takeaways

  • The Fibonacci sequence is a classic example of recursion.
  • Every recursive function must include a base case to stop recursive calls.
  • Recursive Fibonacci is easy to understand but inefficient for large values because it repeats calculations.
  • Input validation helps prevent invalid recursive calls.
  • For large Fibonacci numbers, iterative or dynamic programming approaches are more efficient than recursion.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (325) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (313) Bootcamp (13) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (34) data (10) Data Analysis (42) Data Analytics (31) data management (16) Data Science (412) Data Strucures (23) Deep Learning (208) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (12) flask (4) flutter (1) FPL (17) Generative AI (77) Git (12) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (367) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1419) Python Coding Challenge (1207) Python Mathematics (8) Python Mistakes (51) Python Quiz (585) Python Tips (27) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (54) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)