Python Program to Find the Factors of a Number – 2024

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

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

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