public class PrimeFactorsOfANumber_Java {
static void printPrimeFactor(int n) {
int div = 2;
while (n != 0) {
if (n % div != 0) {
div = div + 1;
}
else {
System.out.print(div + ", ");
n = n / div;
if (n == 1) {
break;
}
}
}
System.out.println();
}
public static void main(String args[]) {
int n = 124;
printPrimeFactor(n); // 2 x 2 x 31
printPrimeFactor(288); // 2 x 2 x 2 x 2 x 2 x 3 x 3
printPrimeFactor(1288); // 2 x 2 x 2 x 7 x 23
printPrimeFactor(893); // 19 x 47
}
}
/*
run:
2, 2, 31,
2, 2, 2, 2, 2, 3, 3,
2, 2, 2, 7, 23,
19, 47,
*/