Java Display the Prime Factorization of a number

前端 未结 7 1568
闹比i
闹比i 2020-11-30 13:02

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:



        
7条回答
  •  一向
    一向 (楼主)
    2020-11-30 13:28

    remove the if (count == 0) {continue;} statement from the while loop and put it after it, in the for loop. :)

    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);
        }
    

提交回复
热议问题