Skip to content
Home » Prime Factors Program using While Loop in Python

Prime Factors Program using While Loop in Python

Learn about Prime Factors Program using While Loop 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.

Prime Factors Program using While Loop in Python

# Python program to find prime factors of a number
 
# input from user
num = int(input('Enter number: '))

# get 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: 543
3 181 are the prime factors of number 543

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Prime Factors of a Number in Python