Tuesday, 28 July 2026

πŸš€ Day 93/150 – Recursive Factorial in Python

 

πŸš€ Day 93/150 – Recursive Factorial in Python

Recursion is a programming technique where a function calls itself to solve a smaller version of the same problem. One of the most common examples of recursion is calculating the factorial of a number.

The factorial of a positive integer n is the product of all positive integers from 1 to n.

Formula:

5! = 5 × 4 × 3 × 2 × 1 = 120

In this post, we'll explore four different ways to calculate the factorial of a number using recursion and related approaches.


Method 1 – Basic Recursive Function

A recursive function calls itself until it reaches the base case.

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(5))





Output

120

Explanation
    If n is 0 or 1, the function returns 1.
  • Otherwise, it returns n × factorial(n - 1).
    The function keeps calling itself until the base case is reached.

Method 2 – Taking User Input

Calculate the factorial recursively using user input.

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) num = int(input("Enter a number: ")) print("Factorial:", factorial(num))









Sample Input
6

Output

Factorial: 720

Explanation
  • The user enters a number.
  • The recursive function calculates its factorial.
  • The result is displayed.

Method 3 – Recursive Function with Error Handling

Prevent negative numbers from being processed.

def factorial(n): if n < 0: return "Factorial is not defined for negative numbers." if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(-3))










Output
Factorial is not defined for negative numbers.

Explanation

  • The function first checks for negative numbers.
  • If the input is negative, it returns an error message.
  • Otherwise, recursion continues normally.

Method 4 – Recursive Function with Default Argument

Use a default argument to calculate the factorial of 5 if no value is provided.
def factorial(n=5): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial()) print(factorial(4))





























Explanation

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

Output

120
24
Explanation

Comparison of Methods

MethodBest For
Basic RecursionLearning recursion
User InputInteractive programs
Error HandlingValidating input
Default ArgumentOptional function parameters

πŸ”₯ Key Takeaways

  • Recursion is a technique where a function calls itself.
  • Every recursive function must have a base case to stop the recursion.
  • Factorial is one of the best examples for understanding recursion.
  • Always validate input before performing recursive calculations.
  • While recursion is elegant, iterative solutions are often more memory-efficient for very large inputs.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (322) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (310) Bootcamp (13) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (33) data (10) Data Analysis (42) Data Analytics (31) data management (16) Data Science (411) Data Strucures (23) Deep Learning (206) 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 (365) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1417) Python Coding Challenge (1206) Python Mathematics (8) Python Mistakes (51) Python Quiz (584) Python Tips (27) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (53) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)