Conversation with Merlin [email protected] · Sun Nov 19 2023

Write a program to print the factorial of a given number with the help of recursion.

Sure, here's a Python program that uses recursion to calculate the factorial of a given number:

def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n-1) # Taking input from the user num = int(input("Enter a number to find its factorial: ")) # Checking if the number is negative if num < 0: print("Factorial cannot be found for a negative number.") elif num == 0: print("The factorial of 0 is 1") else: print("The factorial of", num, "is", factorial(num))

When you run this program, it will prompt you to enter a number, and then it will use recursion to calculate and print the factorial of that number.