Learn about Factorial Program in Python using Function in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Factorial Program in Python using Function
The below code prints the factorial of the number using a User-defined Function in python.
# Python program to find the factorial of a number using function
#user-defined function
def factorial(num):
fact = 1
for i in range(1,num + 1):
fact = fact*i
return fact
# input
num = int(input("Enter number: "))
# check number is positive, negative, or zero
if num < 0:
print('Factorial does not exist for negative numbers')
elif num == 0:
print('The factorial of 0 is 1')
else:
# function call
print('The factorial of',num,'is',factorial(num))
Output:
Enter number: 6
The factorial of 6 is 720
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Python Program to Find Factorial of Number using Recursion