Prime factors in python | We will discuss how to python program find prime factors of a number. The number which has only two factors 1 and itself, those numbers are called the prime number.

Examples:-

- Find the prime factors of a number 150.
- The numbers 1, 2, 3, 5, 6, 10, 15, 25, 30, 50, 75, and 150 are complete divides number 150 so they are called the factor of 150.
- In this numbers 2, 3, and 5 are prime factors of 150. Because these numbers are prime numbers.

## Prime Factors of a Number in Python

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

```
# Python program to find prime factors of a number
# take inputs
num = int(input('Enter number: '))
# find prime factors
for i in range(2, num + 1):
if(num % i == 0):
isPrime = 1
for j in range(2, (i //2 + 1)):
if(i % j == 0):
isPrime = 0
break
if (isPrime == 1):
print(i,end=' ')
print('are the prime factors of number',num)
```

Output for the different input values:-

Enter number: 35

5 7 are the prime factors of number 35

Enter number: 99

3 11 are the prime factors of number 99

Enter number: 150

2 3 5 are the prime factors of number 150

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 prime factors of a number using for-loop and result will be displayed on the screen.

```
for i in range(2, num + 1):
if(num % i == 0):
isPrime = 1
for j in range(2, (i //2 + 1)):
if(i % j == 0):
isPrime = 0
break
if (isPrime == 1):
print(i,end=' ')
print('are the prime factors of number',num)
```

## Prime Factors Program using While Loop

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

```
# Python program to find prime factors of a number
# take inputs
num = int(input('Enter number: '))
# find prime factors
i = 1
while(i <= num):
count = 0
if(num % i == 0):
j = 1
while(j <= i):
if(i % j == 0):
count = count + 1
j = j + 1
if (count == 2):
print(i,end=' ')
i = i + 1
print('are the prime factors of number',num)
```

Output:-

Enter number: 100

2 5 are the prime factors of number 100

## Prime Factorization Python Program 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 prime factors of a number using function
def primeNumber(num): # user defind function
# find prime factors
for i in range(2, num + 1):
if(num % i == 0):
isPrime = 1
for j in range(2, (i //2 + 1)):
if(i % j == 0):
isPrime = 0
break
if (isPrime == 1):
print(i,end=' ')
print('are the prime factors of number',num)
# take inputs
num = int(input('Enter number: '))
# calling function
primeNumber(num)
```

Output:-

Enter number: 500

2 5 are the prime factors of number 500

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

```
def primeNumber(num): # user defind function
# find prime factors
for i in range(2, num + 1):
if(num % i == 0):
isPrime = 1
for j in range(2, (i //2 + 1)):
if(i % j == 0):
isPrime = 0
break
if (isPrime == 1):
print(i,end=' ')
print('are the prime factors of number',num)
```

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
primeNumber(num)
```

Also See:- Leap Year Program 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 do you find anything incorrect? Let us know in the comments. Thank you!