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 chr()
Python Dictionary
Python frozenset()
Deep Learning in Python - LazyProgrammer
Python exec()
Python Program to Display Powers of 2 Using Anonymous Function
Python String translate()
Python Program to Count the Number of Digits Present In a Number
Python Deep Learning Cookbook - Indra den Bakker
Python String replace()
Python Set intersection_update()
Python getattr()
Python Program to Extract Extension From the File Name
Python String splitlines()
Python Program to Swap Two Variables
Python Program to Check Prime Number
How to Get Started With Python?
Python Object Oriented Programming
How to get current date and time in Python?
Python List index()
Python Program to Compute the Power of a Number
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find HCF or GCD
Python String isdigit()
Python Program to Delete an Element From a Dictionary
Python Matrices and NumPy Arrays
Python strftime()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Randomly Select an Element From the List
Python print()
Python String find()