Factorial Program in Python
The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as -
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
The factorial operation generally come across many mathematical operations, like - combinatorics, algebra, analysis.
Example
Suppose, we want to get factorial of 6, then factorial will be -
6! = 6 X 5 X 4 X 3 X 2 X 1
720

Python gets factorial using For Loop
Here is the Python program to find the factorial of a number using for loop. When the user entered the number as input, it passes through the if else statement. If the number is greater than 0, then it passes through the for loop and finds the factorial of the number.
num = int(input("Enter a number : "))
factorial = 1
if num > 0:
for x in range(1,num + 1):
factorial = factorial*x
print("The factorial of",num," : ",factorial)
elif num == 0:
print("The factorial of 0 is 1")
else:
print("Factorial does not exist for given input")
Output of the above code
(env) c:\python37\Scripts\projects>pythonfactorial.py
Enter a number: 6
The factorial of 6 : 720
(env) c:\python37\Scripts\projects>pythonfactorial.py
Enter a number: 5
The factorial of 5 : 120
(env) c:\python37\Scripts\projects>pythonfactorial.py
Enter a number: -1
Factorial does not exist for given input
(env) c:\python37\Scripts\projects>pythonfactorial.py
Enter a number: 0
The factorial of 0 is 1
Python gets factorial using Recursion function
Here is the Python program to find the factorial of a number using the recursion function. When user enters a number as input, it passes to the function. If the number equals to the 1, it returns 1. The factorial of a number less than equals to 0 does not exist. If the input is greater than 1, the function calls recursively and find the factorial of a number.
def factorial(num):
if num == 1:
return num
elif num <= 0:
return " Not Exist"
else:
return num * factorial(num - 1)
# Enter the input
num = int(input("Enter a Number: "))
print("Factorial of", num, " : ", factorial(num))
Output of the above code
(env) c:\python37\Scripts\projects>factorialpython.py
Enter a Number : 3
Factorial of 3 : 6
(env) c:\python37\Scripts\projects>factorialpython.py
Enter a Number : 1
Factorial of 1 : 1
(env) c:\python37\Scripts\projects>factorialpython.py
Enter a Number : 4
Factorial of 4 : 24
(env) c:\python37\Scripts\projects>factorialpython.py
Enter a Number : 0
Factorial of 0 : Not Exist
(env) c:\python37\Scripts\projects>factorialpython.py
Enter a Number : 6
Factorial of 6 : 720
Related Articles
How to draw different shapes using Python PygamePython send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml