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 Inheritance
Python time Module
Python Objects and Classes
Python Program to Print all Prime Numbers in an Interval
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Make a Flattened List from Nested List
Python Set remove()
Debug a JavaMail Program
Python String center()
Python Program to Check If Two Strings are Anagram
Python Program to Represent enum
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python ord()
Python Program to Find the Sum of Natural Numbers
Python bytearray()
Python __import__()
Node.js vs Python for Backend Development
Python Program to Create a Countdown Timer
Python String join()
Python print()
Python Set update()
Python Program to Remove Punctuations From a String
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Find Sum of Natural Numbers Using Recursion
Python Shallow Copy and Deep Copy
Python Function Arguments
Python Program to Find the Size (Resolution) of a Image
Python hasattr()
Python Tuple index()
Python bytes()
APIs in Node.js vs Python - A Comparison