C Program to Find Prime Factors of a Numbers

In this C program, we will calculate all prime factors of a number. A Prime Factor of a number is a factor that is also a prime number. A Prime number is a natural number greater than 1 that is only divisible by either 1 or itself.
For Example: Prime factors of 15 are 3 and 5.

Required Knowledge


C program to print all prime factors of a number using for loop

#include <stdio.h>  
  
int main() {  
    int counter, N, i, isPrime;  
   
    printf("Enter a Number\n");  
    scanf("%d", &N);   
    
    printf("List of Prime Factors of %d\n", N);  
    for(counter = 2; counter <= N; counter++) {   
        if(N%counter==0) {
            isPrime = 1;
            for(i = 2; i <=(counter/2); i++) {
                if(counter%i==0) {
                    isPrime=0;
                    break;
                }
            }
   
            if(isPrime==1)
                printf("%d ", counter);  
        }  
    }  
    return 0;  
}
Output
Enter a Number
15
List of Prime Factors of 15
3 5
Enter a Number
50
List of Prime Factors of 50
2 5

Related Topics
C program to print all prime numbers between 1 to N using for loop
C program to find sum of prime numbers between 1 to N
C program to check whether a number is prime or not
C program to find sum of digits of a number using recursion
C program to reverse a number using recursion
C Program to calculate factorial of a number
C program to find perfect numbers between 1 to N using for loop
C program to find all roots of quadratic equation
C program to print all prime factors of a number
List of all C programs