Google News
logo
Python Programs on Factorial Numbers
Here are two Python programs to calculate the factorial of a given number :

1. Using a for loop :

Program :
num = int(input("Enter a number: "))
factorial = 1

if num < 0:
    print("Factorial does not exist for negative numbers.")
elif num == 0:
    print("Factorial of 0 is 1.")
else:
    for i in range(1, num + 1):
        factorial *= i
    print("Factorial of", num, "is", factorial)
Output :
Enter a number: 6
Factorial of 6 is 720


2. Using recursion :

Program :
def factorial(num):
    if num < 0:
        return "Factorial does not exist for negative numbers."
    elif num == 0:
        return 1
    else:
        return num * factorial(num - 1)

num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
Output :
Enter a number: 6
Factorial of 6 is 720