成都网站建设设计

将想法与焦点和您一起共享

Python程序:寻找整数所有除数

创新互联Python教程:

创新互联是专业的怀化网站建设公司,怀化接单;提供成都网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行怀化网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

编写一个 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

本文名称:Python程序:寻找整数所有除数
文章网址:https://chengdu.cdxwcx.cn/article/dhodjpj.html