Python 程序找出数字阶乘而不使用递归
如果需要找出数字阶乘而不用递归,可以使用 while 循环。
示例
以下展示了相同内容 −
my_num = int(input("Enter a number :")) my_factorial = 1 while(my_num>0): my_factorial = my_factorial*my_num my_num=my_num-1 print("The factorial of the number is : ") print(my_factorial)
输出
Enter a number :7 The factorial of the number is : 5040
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
说明
- 输入数字由用户提供。
- 一个变量指定为 1。
- 检查为 0。
- 如果不是,它乘以变量中的前一个值。
- 它指定为同一个变量。
- 这样做直到数字达到 0。
- 然后,它在控制台上显示为输出。
广告