Java Display the Prime Factorization of a number

You are almost there! Move the if-continue block outside the for loop. Otherwise, it “continues” the inner-most loop, rather than the one you intended.

while (number % i == 0) {
    number /= i;
    count++;
}
if (count == 0) {
    continue;
}
System.out.println(i+ "**" + count);

Alternatively, you could enclose the System.out.println call in if (count != 0), because it’s the only statement following the continue:

while (number % i == 0) {
    number /= i;
    count++;
}
if (count != 0) {
    System.out.println(i+ "**" + count);
}

Your program on ideone: link.

Leave a Comment