1 Star 0 Fork 0

fanyangchu/PythonCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Python Program for factorial of a number 782 Bytes
一键复制 编辑 原始数据 按行查看 历史
Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
Recursive:
# Python 3 program to find
# factorial of given number
def factorial(n):
# single line to find factorial
return 1 if (n==1 or n==0) else n * factorial(n - 1);
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
Iterative:
# Python 3 program to find
# factorial of given number
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fanych/pythoncode.git
git@gitee.com:fanych/pythoncode.git
fanych
pythoncode
PythonCode
master

搜索帮助