Python Program to Find Factors of Number

P

In this program (Python Program to Find Factors of Number), you’ll learn to find the factors of a number using a for loop and display it.

To understand this example, you should have the knowledge of following Python programming topics:

  • Python if…else Statement
  • Python for Loop
  • Python User-defined Functions

Download an Android Application for online learning app of Study Warehouse

Python Program to Find Factors of Number

# Python Program to find the factors of a number

# define a function
def print_factors(x):
# This function takes a number and prints the factors

print("The factors of",x,"are:")
 for i in range(1, x + 1):
 if x % i == 0:
 print(i)

# change this value for a different result.
num = 320

# uncomment the following line to take input from the user
#num = int(input("Enter a number: "))

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 test the program, change the value of num.

In this program, the number whose factor is to be found is stored in num.

Then we display its factors using the function print_factors(). In the function, we use a for loop to iterate from 1 to that number and only print it if, it perfectly divides our number. Here, print_factors() is a user-defined functions

Read Also: Python Program to Check Prime Number

About the author

Pardeep Patel
Pardeep Patel

Add comment

Basics of Python