Realization of recursive factorial in Python3

  • 2021-09-24 23:03:23
  • OfStack

Title

Use recursive method to find 5!

Program analysis

Just call recursively.


def factorial(n):
 return n*factorial(n-1) if n>1 else 1
print(factorial(5))

Supplement: python finds the factorial of N

This topic requires writing a program to calculate the factorial of N

Input format:

The input gives a positive integer N in line 1.

Output format:

Output the factorial value F in line 1 in the format "product = F", noting that there are 1 space on the left and 1 space on the left of the equal sign. The problem ensures that the calculation results do not exceed the double precision range.

Enter sample:

5

Sample output:

product = 120


x = int(input())
a = 1
for i in range(1, x+1):
 a = a*i
print("product = %d" % float(a))

Related articles: