In this program, you will learn how to find the factors of any given number n
# This program to find the factors of the numbers
n = int(input('Enter the number:'))
# n = 480
def find_factors(n):
for i in range(1, n + 1):
if n % i == 0:
print(i)
find_factors(n)
Enter the number:240
The factors of 240 are
1
2
3
4
5
6
8
10
12
15
16
20
24
30
40
48
60
80
120
240
In this program, the given number n is 240. You can change the value of n to find the factors.
The n value 240 passed to the find_factors() function.
We have used for loop to iterate from i to n.
If n is divisible by i then it is factor of n.
In this program, for loop iterates the numbers i from 1 to 240. If 240 is divisible any number i, then it's a factor.
The following topics can be useful to understand this program.