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 Program to Print the Fibonacci sequence
Python Program to Safely Create a Nested Directory
Python Errors and Built-in Exceptions
Python List insert()
Python isinstance()
Python Exception Handling Using try, except and finally statement
Python Program to Check If Two Strings are Anagram
Python File I/O Operation
Python dir()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python List copy()
Python oct()
Python Program to Get Line Count of a File
Python Iterators
Python String join()
Python len()
Python String isspace()
Python Program to Convert Celsius To Fahrenheit
Python Set symmetric_difference_update()
Python String capitalize()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Directory and Files Management
Python list()
Python Program to Swap Two Variables
Python any()
Python Decorators
Python Program to Iterate Over Dictionaries Using for Loop
Python Matrices and NumPy Arrays
Python String format_map()
Python max()
Python Tuple index()
Python Program to Append to a File