English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn to use a for loop to find the factors of a number.
To understand this example, you should understand the followingPython programmingTopic:
# Use Python program to find a number's factors # This function calculates the factors of the passed parameter def print_factors(x): print(x, "has factors:") for i in range(1, x + 1) if x % i == 0: print(i) num = 320 print_factors(num)
Output result
32The factors of 0 are: 1 2 4 5 8 10 16 20 32 40 64 80 160 320
Note:To find the factors of another number, change the value of num.
In this program, the number whose factors are found is stored in num, which is passed to the print_factors() function. This value is assigned to the variable x in print_factors().
In this function, we use a for loop to iterate from i equals x, if x is completely divisible by i, it is a factor of x.