Python Program to Find the Factors of a Number
ⓘ Sponsored by 10xer.co
The factors of a number are the whole numbers that divide that number evenly, without leaving a remainder. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12, because these are the only numbers that can be multiplied together to give 12.
Python Code :
The below Python program finds the factors of a number:
# take input from user
num = int(input("Enter a number: "))
# iterate from 1 to the number and check for factors
factors = []
for i in range(1, num+1):
if num % i == 0:
factors.append(i)
# print the factors
print("The factors of", num, "are:", factors)
In this program, we first take input from the user for the number whose factors are to be found. We then use a for loop to iterate from 1 to the number, checking for factors using the modulus operator (%). If a factor is found, we append it to a list called factors.
Finally, we print the factors of the number using the print() function. The output will be in the format of “The factors of (num) are: (factors)”.
For example :
Enter a number: 40
The factors of 40 are: [1, 2, 4, 5, 8, 10, 20, 40]
ⓘ Sponsored by 10xer.co