Sat Jul 24 2021

Recursive Factorial

File Name: recursive-factorial.py

#!/usr/bin/evn python

# Define function as 'factorial' and pass parameter as 'no'
def factorial(no):
    if no == 0:
        return 1
    else:
        # Call function 'factorial' recursively by passing parameter 'no-1'
        return no * factorial(no - 1)

# Take input from the user
num = int(input("Enter a number to calculate factorial: "))
fctrl = 1

# Check if the number is negative, positive or zero
if num < 0:
    print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    # Call function 'factorial'
    fctrl = factorial(num)
    print("The factorial of",num,"is",fctrl)
    
    
    
#***** Output *****
# Enter a number to calculate factorial: 5
# The factorial of 5 is 120
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.