新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:打印從 1 到 100 的強數(shù)
創(chuàng)新互聯(lián)python教程:

專業(yè)成都網(wǎng)站建設公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!成都創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設,五站合一網(wǎng)站設計制作,服務好的網(wǎng)站設計公司,網(wǎng)站制作、網(wǎng)站設計負責任的成都網(wǎng)站制作公司!
寫一個 Python 程序來打印從 1 到 100,或 1 到 n,或最小到最大的強數(shù),并舉例說明。
打印從 1 到 100 的強數(shù)的 Python 程序
這個 python 程序允許用戶輸入最大限值。接下來,該程序打印從 1 到用戶輸入值的強數(shù)。在這個 python 程序中,首先,我們使用 For Loop 來迭代一個介于 1 和最大值之間的循環(huán)。 蟒內(nèi)為回路
- 我們使用 While Loop 來分割給定的數(shù)字。這樣我們就可以找到數(shù)字中每個數(shù)字的階乘。
- 在 While 循環(huán)中,我們使用階乘函數(shù)來尋找階乘。
- if 語句通過將原始值與階乘之和進行比較來檢查給定的數(shù)是否為強數(shù)。
提示:建議大家參考階乘、強數(shù)文章,了解 Python 邏輯。
# Python Program to print Strong Numbers from 1 to N
import math
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range(1, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)
Sum = Sum + Factorial
Temp = Temp // 10
if (Sum == Number):
print(" %d is a Strong Number" %Number)打印從 1 到 N 的強數(shù)的 Python 程序
在這個程序中,我們允許用戶輸入最小值和最大值。接下來,這個 Python 程序打印介于最小值和最大值之間的強數(shù)
import math
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range(minimum, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)
Sum = Sum + Factorial
Temp = Temp // 10
if (Sum == Number):
print(" %d is a Strong Number" %Number)
Please Enter the Minimum Value: 10
Please Enter the Maximum Value: 100000
145 is a Strong Number
40585 is a Strong Number 新聞名稱:Python 程序:打印從 1 到 100 的強數(shù)
本文鏈接:http://www.5511xx.com/article/dphgjhh.html


咨詢
建站咨詢
