Introduction
Factorial is a mathematical operation that multiplies a given number by every positive integer less than it. The factorial of a number n
is denoted as n!
and is calculated as:
n! = n * (n-1) * (n-2) * ... * 1
0! = 1
(by definition)
Recursion is an effective method to calculate the factorial of a number, as it allows you to express the factorial in terms of smaller factorials. This tutorial will guide you through creating a Python program that calculates the factorial of a number using recursion.
Example:
- Function Name:
factorial
- Function Purpose: Calculate the factorial of a given number using recursion.
- Program Output:
Factorial of 5 is 120
Problem Statement
Create a Python program that:
- Defines a recursive function named
factorial
. - The function takes a single argument,
n
. - The function calculates the factorial of
n
using recursion. - The program calls the function with a sample value to demonstrate its usage.
Solution Steps
- Define the Recursive Function: Use the
def
keyword to define a function namedfactorial
. - Add a Base Case: Include a base case to stop the recursion when
n
equals1
or0
. - Implement the Recursive Case: In the recursive case, call the
factorial
function itself withn-1
. - Call the Function: Call the
factorial
function with a sample value, such as5
. - Display the Result: Print the result returned by the function.
Python Program
# Python Program to Calculate the Factorial Using Recursion
# Author: https://www.rameshfadatare.com/
# Step 1: Define the recursive function
def factorial(n):
# Step 2: Add a base case
if n == 0 or n == 1:
return 1
# Step 3: Implement the recursive case
else:
return n * factorial(n - 1)
# Step 4: Call the function with a sample value
result = factorial(5)
# Step 5: Display the result
print(f"Factorial of 5 is {result}")
Explanation
Step 1: Define the Recursive Function
- The function
factorial
is defined using thedef
keyword. It takes one parameter,n
, which is the number for which the factorial is calculated.
Step 2: Add a Base Case
- The base case checks if
n
is0
or1
. If so, the function returns1
because the factorial of0
and1
is1
. The base case is essential to stop the recursion and prevent an infinite loop.
Step 3: Implement the Recursive Case
- If
n
is not0
or1
, the function calls itself withn-1
. This reduces the problem size and recursively calculates the factorial until the base case is reached.
Step 4: Call the Function
- The
factorial
function is called with the value5
. This initiates the recursion process.
Step 5: Display the Result
- The result of the function is stored in the
result
variable, and theprint()
function is used to display the factorial of5
.
Output Example
Example Output:
Factorial of 5 is 120
Additional Examples
Example 1: Factorial of 0
# Calling the function to calculate the factorial of 0
result = factorial(0)
print(f"Factorial of 0 is {result}")
Output:
Factorial of 0 is 1
Example 2: Factorial of 7
# Calling the function to calculate the factorial of 7
result = factorial(7)
print(f"Factorial of 7 is {result}")
Output:
Factorial of 7 is 5040
Example 3: Factorial of 1
# Calling the function to calculate the factorial of 1
result = factorial(1)
print(f"Factorial of 1 is {result}")
Output:
Factorial of 1 is 1
Conclusion
This Python program demonstrates how to calculate the factorial of a number using recursion. Recursion is a powerful technique that allows you to solve problems by breaking them down into smaller instances of the same problem. Understanding how to implement recursive functions is essential for solving a wide range of problems in Python and other programming languages.