The factorial of a number is the product of all the positive integers less than or equal to the number. For example, the factorial of 5 is = 5 x 4 x 3 x 2 x 1 = 120.
num = int(input("Enter a number: ")) factorial = 1 if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial)
Enter a number: 9
The factorial of 9 is 362880
- We first check if the number is negative.
- If the number is negative we display an appropriate message.
- If the number is positive, we use a for loop to calculate the factorial.
- We then display the factorial of the number.