源代码
# Python Program to find the factors of a number
# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = 320
print_factors(num)
输出
The factors of 320 are: 1 2 4 5 8 10 16 20 32 40 64 80 160 320
注意:要查找另一个数的因数,请更改 num
的值。
在这个程序中,要查找因数的数存储在 num
中,然后将其传递给 print_factors()
函数。该值在 print_factors()
中赋值给变量 x。
在函数中,我们使用 for
循环从 i 等于 x 开始迭代。如果 x 能被 i 整除,则 i 是 x 的一个因数。
另请阅读