Python Program to Find the Factors of a Number

1 min read

The factor of any number is a whole number which exactly divides the number into a whole number without leaving any remainder.

For example, 3 is a factor of 9 because 3 divides 9 evenly leaving no remainder.

Problem

Create a Python program to find all the factors of a number.

Algorithm

Step 1:  Take a number

Step 2: Loop over every number from 1 to the given number

Step 3: If the loop iterator evenly divides  the provided number i.e. number % i == 0 print it.

Program

number = 69

print("The factors of {} are,".format(number))

for i in range(1,number+1):
    if number % i == 0:
        print(i)

Output

The factors of 69 are,
1
3
23
69

Print factors of a user-provided number

number = int(input("Enter a number "))
print("The factors of {} are,".format(number))

for i in range(1,number+1):
    if number % i == 0:
        print(i)

Output

Enter a number  469
The factors of 469 are,
1
7
67
469

PROGRAMS

Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read