;

Python Program to Find the Prime Factors of a Given Number


Tutorialsrack 22/04/2020 Python

In this Python Program, we will learn how to find the prime factors of a given number. For example, the prime factor for 6 is 2, 3.

Here is the source code to find the prime factors of a given number.

Program 1: Using For Loop

Program 1: Using For Loop
# Python Program to find the Prime Factors of a Number

# Take the Input From the USer
Number = int(input("Enter any Number: "))

for i in range(2, Number + 1):
    if(Number % i == 0):
        isprime = 1
        for j in range(2, (i //2 + 1)):
            if(i % j == 0):
                isprime = 0
                break
            
        if (isprime == 1):
            print(" %d is a Prime Factor of a Given Number %d" %(i, Number))
Output

Enter any Number: 15

 3 is a Prime Factor of a Given Number 15

 5 is a Prime Factor of a Given Number 15

 

Enter any Number: 100

 2 is a Prime Factor of a Given Number 100

 5 is a Prime Factor of a Given Number 100

Program 2: Using While Loop

Program 2: Using While Loop

# Python Program to find the Prime Factors of a Number

# Take the input From User
Number = int(input("Enter any Number: "))
i = 1

while(i <= Number):
    count = 0
    if(Number % i == 0):
        j = 1
        while(j <= i):
            if(i % j == 0):
                count = count + 1
            j = j + 1
            
        if (count == 2):
            print(" %d is a Prime Factor of a Given Number %d" %(i, Number))
    i = i + 1
Output

Enter any Number: 100

 2 is a Prime Factor of a Given Number 100

 5 is a Prime Factor of a Given Number 100

 

Enter any Number: 20

 2 is a Prime Factor of a Given Number 20

 5 is a Prime Factor of a Given Number 20


Related Posts



Comments

Recent Posts
Tags