29
Question Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number whose factorial is to be calculated as the argument.
def factorial(n):
product = 1
for i in range(1, n + 1):
product *= i
return product
n = int(input("Enter a number: "))
fact = factorial(n)
print("The factorial of", n, "is", fact)Enter a number: 5
The factorial of 5 is 120.
Enter a number: 0
The factorial of 0 is 1.
def
factorial
(
n
):
product
=
1
for
i
in
range
(
1
,
n
+
1
):
product
*=
i
return
product
n
=
int
(
input
(
"Enter a number: "
))
fact
=
factorial
(
n
)
print
(
"The factorial of"
,
n
,
"is"
,
fact
)
Output
Enter a number: 5
The factorial of 5 is 120.
Enter a number: 0
The factorial of 0 is 1.