Set variable factorial =1 to store factorial of a given number
Iterate numbers starting from 1 to the given number n using for loop and range() function. (here, the loop will run five times because n is 5)
In each iteration, multiply factorial by the current number and assign it again to a factorial variable (factorial = factorial *i)
After the loop completes, print factorial
Solution:
num = 5
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
# run loop 5 times
for i in range(1, num + 1):
# multiply factorial by current number
factorial = factorial * i
print("The factorial of", num, "is", factorial)
Hint:
factorial =1to store factorial of a given numbernusing for loop andrange()function. (here, the loop will run five times becausenis 5)factorial = factorial *i)factorialSolution:
need an explanation for this answer? contact us directly to get an explanation for this answer