Learn about Prime Factors of a Number in Python in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Prime Factors of a Number in Python
# Python program to find prime factors of a number
# input from user
num = int(input('Enter a number: '))
# get 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:
Enter a number: 4
2 are the prime factors of number 4
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Reverse a Number in Python using Slicing