# Thread: Find Prime Factors Of Given NUmber

1. ## Find Prime Factors Of Given NUmber

how to write i tried as follows but it prints all numbers.
i want to print only prime factors of number, also i want it using for loop not while loop can anyone help me. also plz explain me each step bcoz im new in using for loop

Code:
```#include<stdio.h>int main(){
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
for(i=1;i<=num;i++){
if(num%i==0)
prime(i);
printf("%d\n",i);
}
}
prime(int i)
{
int j,flag;
for(j=2;j<i;j++)
{
if(j%i==0)
flag=1;
break;
if(flag==1)
return 0;
return i;
}
}``` 2. You can actually structure this algorithm in such a way that a primality test is not necessary. Simply find the smallest number that divides num, and then divide as much as you can, so that the divisor HAS to be prime.
Code:
```void factorize(long num)
{
long i = 2;
while(num > 1) {
while(num % i == 0) {
num /= i;
printf("%d  ", i);
}
++i;
}
printf("\n");
}``` 3. Hodor likes prime numbers. They are the only real natural numbers after all. Popular pages Recent additions prime 