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 printf and scanf functions
- For loop in C
- C Program to find factors of a number
- C program to check prime numbers
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