Python Examples

Python Program to Find the Factors of a Number

A whole number that divides the other number completely without leaving any remainders is known as a factor of that number. To find factors of a number python understand the logic below:

For example,

When 9 is divided by 3, there is no remainder left behind. Hence 3 is a factor of 9.  Similarly, a number can have two or more factors. Let us consider 6. 

When 2 divides 6 there is no remainder, and when 3 divides 6 there is no remainder left either because they divide 6 evenly. Therefore we can derive the conclusion that the factors of 6 are 2,3. 

find factors of a number python

Source Code

How do you find the factors of a number in Python?

To find factors of a number python, we can either use the for or the while loop. 

How do you find factors of a number quickly?

The factors of a number are integers when multiplied together to derive the original number itself. To find the factors of a number easily follow the steps given below:

Step 1: Use the prime factorization method to split the prime factors of the number

Step 2: After deriving the prime numbers, take the exponents of all the prime numbers and add 1 to each exponent. 

Step 3: After taking the sum, multiply the exponents together. 

For example:

Consider the number 48. After the prime-factorization method, the prime factors that are derived are 2^4 x 3^1. After adding 1 to the exponents, the modified exponents are 5 and 2. 

Now we multiply 5 and 2.

5×2=10.

Therefore 48 has 10 factors. 

How do you find the factors of a number Program? 

We used the for loop to find the factors of the number in the program below. Read the steps given below for a more detailed explanation. 

Step 1: Choose any number

Step 2: Put every number ranging from 1 to that number in a loop

Step 3: If the number is divided evenly, without leaving any remainder behind then that is a factor. 

                    

Source Code:

numb=int(input("enter any number"))

facts=[]

for a in range(1,numb+1):

    if numb%a==0:

       facts.append(a)

print ("Factors of {} = {}".format(numb,facts))

Output

enter a number 6

Factors of 6 = [2, 3]

How do you find the factors of a number using a while loop in Python?

In the program given below, we used the while loop to find the factors of a given number. This approach differs when compared to the for loop. Initially, we initialize the variable ‘a’ as 1 before starting the loop. We iterate the while loop till a <a+1. Now we check if the current value is a divisor of z or not. If yes, we print the number and increment the value of ‘a’. If not, we skip the number and continue with the next number. 

                    

def factors(z): 
a = 1 

while(i < z+1): 

if z % a == 0: 

print(a) 

a = a + 1 

numb = int(input("Enter any numb : ")) 

print("The factors for {} are : ".format(numb)) 

factors(numb)

Output:

Enter any numb : 20 

1 

2 

4 

5 

10 

20

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.