Python Program to find Factors of a Number

Write a Python Program to find Factors of a Number using While Loop, For Loop, and Functions with an example. We need a loop to iterate the digits from 1 to a given number. Next, check each digit against the number to see whether it is divisible or not. If True, print that digit.

Python Program to find Factors of a Number using while Loop

It allows users to enter any integer value. Next, this program finds Factors of that number using a While Loop. Remember, Integers that are entirely divisible by a given integer (it means remainder = 0) are called factors.

number = int(input("Please Enter any Integer : "))

value = 1
print("The Result of a Given {0} are:".format(number))

while (value <= number):
    if(number % value == 0):
        print("{0}".format(value))
    value = value + 1
Python Program to Find Factors of a Number using while loop

Within the Python language while loop, there is an Python If statement to check whether the Number divisible by value is exactly equal to 0 or not. If it is true, it prints that integer. Otherwise, it skips that integer and checks the next integer. Here, number= 4, value = 1

First Iteration of the Program to find Factors of a Number using a while loop.
(value<= number) – It means (1 <= 4) is True
Now, Check the if condition
if (number%value == 0)  => (4 % 1 ==0) – This condition is TRUE. So, 1 printed

value = value+1 – means the value becomes 2.

Second Iteration: value= 2 and Number= 4 – It means (2 <= 4) is True
if(4 % 2 ==0) – This condition is TRUE. So, 2 printed

Third Iteration: value= 3 and Number= 4 – It means (3 <= 4) is True
if(3 % 2 ==0) – Condition is FLASE. So, 3 Skipped

Fourth Iteration: i = 4 and Number= 4 – It means (4 <= 4) is True
if(4 % 4 ==0) – Condition is TRUE. 4 printed

Next, the value becomes 5 – It means condition (5 <= 4) is False. So, the loop was Terminated. The result of a given 4 = 1, 2, 4.

Python Program to find Factors of a Number using For Loop

In this program, we just replaced the Python While Loop with the Python For Loop.

val = int(input("Please Enter any Value : "))

print("Result of a Given {0} are:".format(val))

for i in range(1, val + 1):
    if(val%i == 0):
        print("{0}".format(i))
Program to find Factors of a Number using for loop

Program to Find Factors of a Number using Functions

This program is the same as the above example. But in this Python program, we separated the logic by defining a new function called FindFac.

def FindFac(num):
    for value in range(1, num + 1):
        if(num % value == 0):
            print("{0}".format(value))

num = int(input("Please Enter any : "))

print("Result of a Given {0} are:".format(num))
FindFac(num)
Program to Find Factors of a Number using functions

Program to find factors of a number using list comprehension

A list comprehension provides a one-line coding solution to find factors of a given number. It’s the same logic that we used in a for loop example but packed inside a list. Here, the if condition filters the factors, and a list comprehension adds them to a list. Refer to the Python list comprehensions.

n = int(input("Enter Number = "))

res = [i for i in range(1, n + 1) if n % i == 0]
print(res)
Enter Number = 32
[1, 2, 4, 8, 16, 32]

Program to find factors of a number using square root

The built-in isqrt() function returns the integer version of the square root. Next, Python set() will remove the duplicate entries. Here, the for loop will iterate up to the square root of the given number, so it is an optimized solution. However, we have to apply the Python sorted() function to the set, etc. Refer to the Python sqrt.

import math
def findFactors(n):
res = set()
for i in range(1, math.isqrt(n) + 1):
if n % i == 0:
res.add(i)
res.add(n // i)
return sorted(res)

nm = int(input("Enter Number = "))
print(findFactors(nm))
Enter Number = 50
[1, 2, 5, 10, 25, 50]

Also Read