編寫一個 Python 程序,使用 for 循環查找整數或數字的所有除數。在這個 Python 示例中,for 循環從 1 迭代到給定的數字,並檢查每個數字是否可以被數字完全整除。如果為真,則將該數作為除數列印出來。
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 循環查找整數的所有除數。
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
在這個 Python 示例中,我們創建了一個 find 除數函數,它將查找並返回給定數字的所有除數。
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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/306441.html