Introduction:
The program is a factorial calculator implemented using recursion in Python. It allows the user to input a number and calculates its factorial. The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n.
Explanation:
- The program begins with an introduction line that displays the heading “Factorial Calculator using Recursion”.
- A recursive function named factorial is defined, which takes an argument n. This function calculates the factorial of n using recursion.
- The factorial function contains a base case: if n is equal to 0, it returns 1. This handles the termination condition of the recursion.
- For any other value of n, the function recursively calls itself with the argument n – 1 and multiplies the result by n. This step continues until the base case is reached.
- After defining the factorial function, the program prompts the user to enter a number using the input function and converts it to an integer using int().
- The program then checks if the input number is less than 0. If so, it prints a message stating that the factorial is not defined for negative numbers.
- If the input number is 0, the program prints a message stating that the factorial of 0 is 1. This covers the special case where the input is 0.
- For any positive input number, the program calls the factorial function with the input number as the argument and stores the result in the variable result.
- Finally, the program displays the factorial of the input number by printing a message that includes the input number and the calculated result.
The program demonstrates the concept of recursion by calculating the factorial of a number. Recursion is a programming technique in which a function calls itself to solve a smaller version of the problem until a base case is reached. In this case, the base case is when the input number becomes 0.
Note: Recursion can lead to stack overflow errors if the recursion depth becomes too large. However, for small input numbers, the program should work correctly.
Algorithm:
- Start by printing an introduction to the factorial calculator.
- Define a recursive function named factorial that takes an argument n.
- If n is equal to 0, return 1 since the factorial of 0 is defined as 1.
- Otherwise, return n multiplied by the factorial of n – 1.
- Ask the user to input a number.
- Check if the number is negative. If so, print a message that the factorial is not defined for negative numbers.
- If the number is 0, print a message stating that the factorial of 0 is 1.
- Otherwise, calculate the factorial of the input number using the factorial function and store the result in the result
- Print the factorial of the input number.
Program:
# Introduction
print("Factorial Calculator using Recursion")
print("------------------------------------")
# Recursive function to calculate the factorial
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
# Getting input from the user
num = int(input("Enter a number: "))
# Checking if the input is valid
if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1")
else:
result = factorial(num)
print("Factorial of", num, "is", result)
Input :
Enter a number : 5
Output :
Factorial of 5 is 120