Python Program to Find Factorial of a Number Using FOR Loop

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.

Program Code

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")
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

Input Given:

Enter a number: 9

Expected Output:

The factorial of 9 is 362880

Code Explanation

  1. We first check if the number is negative.
  2. If the number is negative we display an appropriate message.
  3. If the number is positive, we use a for loop to calculate the factorial.
  4. We then display the factorial of the number.

Leave a Comment

Share to...