Learn about Factorial Program in Python using For Loop 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 For Loop
The below code prints the factorial of a number using a For Loop. Factorial of a number is nothing but the multiplication of all-natural numbers till the given number.
Source code:
# Python program to find the factorial of a number
# user 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:
fact = 1
for i in range(1,num + 1):
fact = fact*i
print('The factorial of',num,'is',fact)
Output:
Enter number: 5
The factorial of 5 is 120
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Find Factors of Number using Function in Python