So for my assignment, I have to write a program that asks the user for an integer input and then print out that number\'s prime factorization. This is what I have:
For one thing, your continue is inside the while loop, where it has no effect whatsoever. The minimal fix would be
public class PrimeFactor {
public static void main(String[] args) {
System.out.print("Enter a positive number: ");
Scanner scanner = new Scanner (System.in);
int number = scanner.nextInt();
int count;
for (int i = 2; i<=(number); i++) {
count = 0;
while (number % i == 0) {
number /= i;
count++;
}
if (count == 0) {
continue;
}
System.out.println(i+ "**" + count);
}
}
}
But you have some other problems:
continue in this case) when an if would sufficeBetter code would be
public class PrimeFactor {
public static void main(String[] args) {
System.out.print("Enter a positive number: ");
Scanner scanner = new Scanner (System.in);
printFactors(scanner.nextInt());
}
public static void printFactors(int product) {
for (int factor = 2; factor <= product; factor++) {
int exponent = 0;
while (product % factor == 0) {
product /= factor;
exponent++;
}
if (exponent > 0) {
System.out.println(factor+ "**" + exponent);
}
}
}
}