Program to find factorial of Number in Python


In this post, we will explore how to find the factorial of a number in Python.

Program to find factorial of Number in Python

In this example, we are taking the input number from the User using the input() function and then calculating the factorial of that number. By default input is a string, So using int we are converting it to an integer.

Next step, we are checking if the number is less than 0. If it is then printing a message “Please, enter the positive number”. Because the factorial of negative number can’t be calculated. If the number is 0 then print the message “factorial of number 0 is 1”.

We are using for loop from 1 to number +1 and calculating factorial using this formula “factorial = factorial*num”.

To understand it better, Please go through these statements.

Python Program

number = int(input("Enter the number to find factorial:"))


factorial = 1

# check if the number is negative, positive or zero
if number < 0:
   print("Please, enter postive number")
elif number == 0:
   print("factorial of number 0 is 1")
   for num in range(1,number + 1):
       factorial = factorial*num
   print(f"Factorial of number {number} is {factorial}")


Enter the number to find factorial:5
Factorial of number 5 is 120