当前位置:   article > 正文

python 数字的阶乘_Python程序使用循环查找数字阶乘

python 输入一个数,求这个数的阶乘 阶乘 4! = 1*2*3*4的结果

python 数字的阶乘

Here you will get python program to find factorial of number using for and while loop.

在这里,您将获得python程序,以使用for和while循环查找数字的阶乘。

Factorial of a number is calculated by multiplying it with all the numbers below it starting from 1.

数字的阶乘是通过将其乘以1以下的所有数字来计算的。

For example factorial of 4 is 24 (1 x 2 x 3 x 4).

例如,阶乘4是24(1 x 2 x 3 x 4)。

Below program takes a number from user as an input and find its factorial.

下面的程序将用户的数字作为输入并找到其阶乘。

Python程序查找数字阶乘 (Python Program to Find Factorial of Number)

使用For循环 (Using For Loop)

  1. num = int(input("enter a number: "))
  2.  
  3. fac = 1
  4.  
  5. for i in range(1, num + 1):
  6. fac = fac * i
  7.  
  8. print("factorial of ", num, " is ", fac)

Output

输出量

enter a number: 5 factorial of 5 is 120

输入数字:5的5 阶乘是120

使用While循环 (Using While Loop)

  1. num = int(input("enter a number: "))
  2.  
  3. fac = 1
  4. i = 1
  5.  
  6. while i <= num:
  7. fac = fac * i
  8. i = i + 1
  9.  
  10. print("factorial of ", num, " is ", fac)

Output

输出量

enter a number: 4 factorial of 4 is 24

输入数字:4的 阶乘为24

影片教学 (Video Tutorial)

翻译自: https://www.thecrazyprogrammer.com/2017/04/python-program-find-factorial-number-using-loop.html

python 数字的阶乘

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/212726
推荐阅读
相关标签
  

闽ICP备14008679号