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 Dictionary
Python Program to Access Index of a List Using for Loop
Python Set symmetric_difference()
Python Keywords and Identifiers
Python Program to Illustrate Different Set Operations
Python Program to Remove Duplicate Element From a List
Python isinstance()
Python Program to Find the Factorial of a Number
Python Modules
Python Set add()
Python Dictionary fromkeys()
Python Program to Convert Kilometers to Miles
Python String format()
Python Program to Solve Quadratic Equation
Python Dictionary pop()
Python Program to Transpose a Matrix
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String translate()
Python File I/O Operation
Python String ljust()
Python all()
Python getattr()
Python Program to Count the Number of Occurrence of a Character in String
Python Global Keyword
Intelligent Projects Using Python - Santanu Pattanayak
Machine Learning with Python for everyone - Mark E.Fenner
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Reverse a Number
Python next()
Python Set isdisjoint()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Sort a Dictionary by Value