Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Find the Factorial of a Number using For Loop


This examples shows, how to find factorial number. The factorial of a number is the product of all the integers from 1 to that number.

Steps
  1. Get input positive integer from user
  2. In Each iteration of current value in factorial is multiplied by 'i'.After for loop function calculate the factorial.
Example:
Example.py
a = int(input("Enter a number: "))
factorial = 1
if a < 0:
   print(" Factorial does not exist for negative numbers")
elif a == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,a + 1):
       factorial = factorial*i
   print("The factorial of",a,"is",factorial)

Output 1

Enter a number: 5
The factorial of 5 is 120

Output 2

Enter a number: -8
Factorial does not exist for negative numbers

Prev Next