Java Display the Prime Factorization of a number

前端 未结 7 1599
闹比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:21

    Not sure why you are printing the multiplication twice! Here is the cleaned up code:

    public static void printPrimeNumbers(int prime) {
    
        int n;
    
        for (int i = 2; i <= prime; i++) {
            n = 0;
            while (prime % i == 0) {
                prime /= i;
                n++;
    
            }
    
            if (n != 0) {
                for (int j = n; j > 0; j--) {
                    System.out.print(i);
    
                    if (prime != 1) {
                        System.out.print("*");
                    }
                }
            }
        }
    }
    

提交回复
热议问题