Factorial program in python using recursion

Last updated 1 week, 5 days ago | 29 views 75     5

Tags:- Python

Python | Program to find factorial of a number using recursive function

This is an example of how to find the factorial of a number in python using a recursive function.

The factorial of a number is a product of all the positive integers less than or equal to that number.
For example, the factorial of 5 is = 5x4x3x2x1 = 120

Factorial is not defined for negative integers, and the factorial of zero is one, 0! = 1.

# recursive function for factorial 
def fact(n):
    if n == 1:
        return n
        return n*fact(n-1)
# accept input from user
n = int(input('Enter no:.'))

# check condition for different input
if n < 0:
   print(f"Sorry, factorial does not exist for {n}, negative number")
elif n == 0:
   print("The factorial of 0 is 1")

Output for the above code

====================== RESTART: E:\py\factorial.py ======================
Enter no.:5
====================== RESTART: E:\py\factorial.py ======================
Enter no.:0
The factorial of 0 is 1
====================== RESTART: E:\py\factorial.py ======================
Enter no.:-5
Sorry, factorial does not exist for -5, negative number