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 Recursion
Python Program to Remove Punctuations From a String
Python Program to Convert Kilometers to Miles
Python frozenset()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String strip()
Python Program to Print the Fibonacci sequence
Python pass statement
Python ascii()
Python Program to Randomly Select an Element From the List
Python Program to Find the Square Root
Python Dictionary setdefault()
Python Program to Convert Celsius To Fahrenheit
Python String rsplit()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Tuple
Python Program to Check if a Key is Already Present in a Dictionary
Python Tuple count()
Python Iterators
Python print()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python strftime()
Python Program to Illustrate Different Set Operations
Python Dictionary keys()
Python Program to Safely Create a Nested Directory
Python String join()
Python Program to Find the Factorial of a Number
Python String rjust()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Make a Simple Calculator
Python Directory and Files Management