# How to Find Factors of a Number in Python

This article will describe several Python programs for showing and find factors of a number given by the user at run-time. The following is a list of the methods that we will use:

1. Find Factors of a Number using While Loop
2. Using for Loop
3. Find Factors of a Number Using Function
4. Using Class

## Method 01: Find Factors of a Number using While Loop

In Python, to determine factors of any number, you must first ask the user to enter a number, then locate and display its factors, as illustrated in the program below. Write a Python program that uses the while loop to find factors of a number.

``````print("Enter the Number: ")
num = input()

num = int(num)
print("\nFactors of", num)

i = 1
while i<=num:
if num%i==0:
print(i)
i = i+1``````

Output:

Now let’s have a look at the modified version of the above program:

This program is a modified version of the previous one. The end is used in this application to avoid printing an automatic newline. To convert a value to a string type, use str(). Invalid input is handled using the try-except method.

``````print("Enter a Number: ", end="")
try:
num = int(input())

print("\nFactors of " +str(num)+ " are: ", end="")
i = 1
while i<=num:
if num % i == 0:
print(i, end=" ")
i = i + 1
print()
except ValueError:
print("\nInvalid Input!")``````

Output:

When we give input in decimal the compiler will print the following output

## Method 02: Find Factors of a Number Using for Loop

This program does the same thing as the last one, but instead of utilizing the while loop, it uses the for loop. First, let’s have a look at the program:

``````print("Enter a Number: ", end="")
try:
num = int(input())

print("\nFactors of " +str(num)+ " are: ", end="")
for i in range(1, num+1):
if num % i == 0:
print(i, end=" ")
print()
except ValueError:
print("\nInvalid Input!")``````

Output:

In the above program, the following code:

``for i in range(1, num+1):``

says that the statement within it will be run num times, with i ranging from 1 to num. If num is 6, for example, the loop is evaluated six times with i values ranging from 1 to 6.

## Method 03: Find Factors of a Number Using Function

This program was made with the FindFact() user-defined function. This function takes as its argument the number entered by the user and outputs all of its factors from inside it.

``````def FindFact(n):
for i in range(1, n+1):
if n % i == 0:
print(i, end=" ")
print()

print("Enter a Number: ", end="")
try:
num = int(input())
print("\nFactors of " +str(num)+ " are: ", end="")
FindFact(num)
except ValueError:
print("\nInvalid Input!")``````

Output:

## Method 04: Find Factors of a Number Using Class

``````class Softhunt:
def FindFact(self, n):
for i in range(1, n+1):
if n % i == 0:
print(i, end=" ")

print("Enter a Number: ", end="")
try:
num = int(input())
print("\nFactors of " +str(num)+ " are: ", end="")
ob = Softhunt()
ob.FindFact(num)
print()
except ValueError:
print("\nInvalid Input!")``````

Output:

A Softhunt object is require to use the FindFact() member method of a class called Softhunt. As a result, an object of type Softhunt is generate, and I’ve accessed the class’s member function using the. (dot) operator, as seen in the program above.

## FAQs

### How many factors does 1080 have?

32 factors: Factors of 1080 is the list of integers that we can split evenly into 1080. There are 32 factors of 1080, of which the following are its prime factors 2, 3, 5. The Prime Factorization of 1080 is 23 × 33 × 51.

### What is a factor in Python?

Any number’s factor is a whole number that divides the number exactly into a whole number without leaving any remainder. 3 is a factor of 9 because it evenly divides 9 and leaves no remainder.

## Conclusion

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

1. Python Count