1. 程式人生 > >遞歸求階乘

遞歸求階乘

== tor code ret %d result turn number 遞歸

def factorial(n): result = n for i in range(1, n): result *= i return result number = int(input("請輸入一個正整數:")) result = factorial(number) print("%d的階乘是:%d" %(number, result)) -----
def factorial(n):
    if n == 1:
        return 1
    else:
        return n* factorial(n - 1)
number = int(input("請輸入一個正整數:"))

result = factorial(number)
print("%d的階乘是:%d" %(number, result))

遞歸求階乘