Objective: Write a program in python to print factorial of a number using for loop.
Code:
# Prompt user for input
num = int(input("Enter a number: "))
# Check if the number is negative, zero, or one
if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0 or num == 1:
print("The factorial of", num, "is 1.")
else:
factorial = 1
# Loop through numbers from 2 to num (inclusive)
for i in range(2, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)
No comments:
Post a Comment