In this program, you’ll learn to find the factors of a number using the for loop.
To understand this example, you should have the knowledge of the following Python programming topics:
Source Code
# 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)
Output
The factors of 320 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 factor is to be 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 the function, we use the for
loop to iterate from i equal to x. If x is perfectly divisible by i, it’s a factor of x.
Related posts:
Python locals()
Python Program to Remove Punctuations From a String
Python List remove()
Python String isidentifier()
Python String isdecimal()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Numbers, Type Conversion and Mathematics
Python Program to Convert Kilometers to Miles
Python String rfind()
Python Program to Swap Two Variables
Python Program to Find the Largest Among Three Numbers
Python Program to Get File Creation and Modification Date
Python staticmethod()
Deep Learning in Python - LazyProgrammer
Python Program to Return Multiple Values From a Function
Python Machine Learning - Sebastian Raschka
Python File I/O Operation
Python String endswith()
Python Variables, Constants and Literals
Python Set add()
Python Tuple count()
Python Deep Learning Cookbook - Indra den Bakker
Python strftime()
Python Dictionary clear()
Python Program to Display Calendar
Python Program to Parse a String to a Float or Int
Python Dictionary pop()
Python Set issubset()
Python String replace()
Python Set difference_update()
Python Program to Find Armstrong Number in an Interval
Python Program to Get a Substring of a String