Python Program to Find the Factors of a Number – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Find the Factors of a Number.

Code

# Python Program to Find the Factors of a Number

#function to compute the factors of the number passed
def print_factors(x):
    print(f"The factors of {x} are:")
    for i in range(1, x + 1):
        if x % i == 0:
            print(i)

num = int(input("Enter a positive number -> "))

print_factors(num)

Output

Enter a positive number -> 24
The factors of 24 are:
1
2
3
4
6
8
12
24
Enter a positive number -> 54
The factors of 54 are:
1
2
3
6
9
18
27
54
Enter a positive number -> 98
The factors of 98 are:
1
2
7
14
49
98
  • In this program, we are simply traversing from 1 to the input number.
  • And we continuously keep on checking, if the input number is directly divisible by the current number or not.
  • If it is directly divisible, then it’s a factor.

Check out our other python programming examples

Leave a Comment

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