# Python Prime Factors Program

By | May 9, 2021

Python Prime Factors Program – This Python program will take a number as input from the user. It will display prime factors of that number.

We will study prime factors before writing the Python program to find them. ### Definition of Prime Factor

A factor that is a prime number is called a Prime Factor.

We can say that prime factors are any set of prime numbers that will give the original number when multiplied.

Example: Find prime factors of 15

The prime factors of 15 are 3 and 5.
since 3×5=15
Note that 3 and 5 are prime numbers.

Question: Find prime factors of 15

The prime factors of 12 are 2, 2 and 3.
since 2x2x3=12
Note that 2, 2 and 3 are prime numbers.

Another Example: Find prime factors of 15

The prime factors of 35 are 5 and 7.
since 5×7=35
Note that 5 and 7 are prime numbers.

## Python Programming Constructs Used in this Program

• User Defined functions in Python. No doubt the use of user defined functions reduce the code complexity and improves code reuse.

Python User Defined Function

Example

## Source Code of Python Prime Factors Program

```# Write a Python Prime Factors program
# that asks the user to enter a number.
# The program will all prime factors
# of that Number.

# Author: www.EasyCodeBook.com (c)

def prime_factors(n):
i = 2
while i<=n:
if n%i==0:
print(i,  end='   ')
n = n // i
else:
i+=1

# Get a number from user

num = int(input('Enter a number to find Prime factors:'))

print('The prime factors of ',num,' are: ', end='')
prime_factors(num)
```

### Output of Python Prime factors Program

```Enter a number to find Prime factors:12
The prime factors of  12  are: 2   2   3

(2)

Enter a number to find Prime factors:60
The prime factors of  60  are: 2   2   3   5

(3)

Enter a number to find Prime factors:35
The prime factors of  35  are: 5   7

```

This Python program explains the concept of prime factorization of a positive number with the help of definition and a lot of examples.

Moreover the code is quite easy to understand and simple to write. Keep visiting EasyCodebook.com for perfect Python programming tutorials to improve your programming logic.

2,116 total views,  3 views today