Learn about Factorial Program in Python using While 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 While Loop
# Python program to find the factorial of a number
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
i = 1
while(i <= num):
fact = fact*i
i = i+1
print('The factorial of',num,'is',fact)
Output:
Enter number: 4
The factorial of 4 is 24
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Factorial Program in Python using For Loop