新聞中心
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)是專業(yè)的懷化網(wǎng)站建設(shè)公司,懷化接單;提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行懷化網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
編寫一個(gè) Python 程序,使用 for 循環(huán)查找整數(shù)或數(shù)字的所有除數(shù)。在這個(gè) Python 示例中,for 循環(huán)從 1 迭代到給定的數(shù)字,并檢查每個(gè)數(shù)字是否可以被數(shù)字完全整除。如果為真,則將該數(shù)作為除數(shù)打印出來(lái)。
num = int(input("Please enter any integer to find divisors = "))
print("The Divisors of the Number = ")
for i in range(1, num + 1):
if num % i == 0:
print(i)
Python 程序使用 while 循環(huán)查找整數(shù)的所有除數(shù)。
num = int(input("Please enter any integer to find divisors = "))
print("The Divisors of the Number = ")
i = 1
while(i <= num):
if num % i == 0:
print(i)
i = i + 1
Please enter any integer to find divisors = 100
The Divisors of the Number =
1
2
4
5
10
20
25
50
100
在這個(gè) Python 示例中,我們創(chuàng)建了一個(gè) find 除數(shù)函數(shù),它將查找并返回給定數(shù)字的所有除數(shù)。
def findDivisors(num):
for i in range(1, num + 1):
if num % i == 0:
print(i)
# End of Function
num = int(input("Please enter any integer to find divisors = "))
print("The Divisors of the Number = ")
findDivisors(num)
Please enter any integer to find divisors = 500
The Divisors of the Number =
1
2
4
5
10
20
25
50
100
125
250
500 當(dāng)前文章:Python程序:尋找整數(shù)所有除數(shù)
地址分享:http://www.5511xx.com/article/dhodjpj.html


咨詢
建站咨詢
