Program to Find Factors of a Number in Python

Program to find factors of a number in python | A number which divides completely the number “N” is called the factor of the number “N”. For example:- the numbers 1, 2, 3, 4, 6, and 12 are complete divides number 12 so they are called the factor of 12. Completly divisible means when we divide the number then it gives result zero.

The number which has only two factors 1 and itself, those numbers are called the prime number. The numbers which have more than two factors are called composite numbers.

To find the factor of a positive number “N” divide that number using natural numbers 1 to “N”. If the number is divisible by the natural number then that natural number is the factor. A number N can have factors only in between 1 to N.

Steps to find the factors of a number:-
1) Take a number N as input
2) Take an iterator variable and initialize it with 1
3) Dividing the number N with an iterator variable
4) If it is divisible then it is a factor of the given number N
5) Increase the iterator variable
6) Repeat the 4 and 5 steps until the iterator variable becomes equal to N.

Example:-
Input:- num = 10
Output:- 1 2 5 10

Program to Find Factors of a Number in Python

This is the simplest and easiest way to find factors of a number program in python. We will take a number while declaring the variables. Python program to find factors of a number using for-loop and display result on the screen.

# Python program to find factors of a number

# take inputs
num = int(input('Enter number: '))

# find factor of number
print('The factors of', num, 'are:')
for i in range(1, num+1):
    if(num % i) == 0:
        print(i, end=' ')

Output for the different input values:-

Enter number: 20
The factors of 20 are:
1 2 4 5 10 20

Enter number: 100
The factors of 100 are:
1 2 4 5 10 20 25 50 100

Enter number: 225
The factors of 225 are:
1 3 5 9 15 25 45 75 225

In this program, inputs are scanned using the input() function and stored in the variable num.

num = int(input('Enter number: '))

Python program to find factors of a number using for-loop and display result on the screen.

print('The factors of', num, 'are:')
for i in range(1, num+1):
if(num % i) == 0:
print(i, end=' ')

we use the for-loop to iterate from i equal to num. If num is perfectly divisible by i, it’s a factor of x.

Find Factors of a Number in Python using While Loop

In the previous program, find factors of a number using for loop but in this program, find factors of a number using while loop.

# Python program to find factors of a number

# take inputs
num = int(input('Enter number: '))

# find factor of number
print('The factors of', num, 'are:')
i = 1
while (i <= num):
    if(num % i == 0):
        print(i, end=' ')
    i = i+1

Output:-

Enter number: 500
The factors of 500 are:
1 2 4 5 10 20 25 50 100 125 250 500

Find Factors in Python using Function

We can also take the help of a function to find the average of 3 numbers in python. A function is a block of code that performs a specific task.

# Python program to find factors of a number using function

def find_factors(num):  #user-defined function
   print('The factors of', num,'are:')
   for i in range(1, num + 1):
       if num % i == 0:
           print(i, end=' ')

# take inputs
num = int(input('Enter number: '))

# calling function
find_factors(num)

Output:-

Enter number: 120
The factors of 120 are:
1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120

In this program, we will be the first defined function.

def find_factors(num):  #user-defined function
   print('The factors of', num,'are:')
   for i in range(1, num + 1):
       if num % i == 0:
           print(i, end=' ')

Inputs are scanned using the input() function and stored in variable num. Then call the function and display the result.

# take inputs
num = int(input('Enter number: '))

# calling function
find_factors(num)

Also See:- Multiplication Table in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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