Question & Answer: this is my code, can you help me fix part 3 that I mark as Bold to vvvvvvvvvvvvv For a given integer n>1, output its prime factorization. E.g. n=8, output: 2^3; n=72, output: 2^3*3^2……

this is my code, can you help me fix part 3 that I mark as Bold to vvvvvvvvvvvvv

For a given integer n>1, output its prime factorization. E.g. n=8, output: 2^3; n=72, output: 2^3*3^2.

—————-

import java.util.Scanner;

public class Main {

 

public static boolean Isprime(int N){ // check if N is prime

boolean mark = true; //default

 

if(N==1)return false; //not a prime

 

for(int i=2;i<N-1;i++){

if(N% i == 0){

mark = false;

break;

}

}

return mark;

}

 

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println(“Enter N for #1 and #2 : “);

int N = sc.nextInt();

 

int count = 0;

int k = 0;

System.out.println(“N is : ” + N);

System.out.println(N + ” Primes Are: “);

while(true){

if(count >0 && Isprime(count)){

System.out.println(count + ” “);

++k;

}

if(k == N) break;

++count;

}

 

System.out.println(“2. primes not exceeding ” + N +” : “);

for(int i =1; i<=N; ++i){

if(Isprime(i)){

System.out.println(i+ “”);

}

}

 

System.out.println(“Enter new N for question 3: “);

       Scanner nsc = new Scanner(System.in);

       int n = nsc.nextInt();

       primefactors(n);

   }

  

   public static void primefactors(int n){

       int count1 = 0; // print the number of 2s divide in

      

       while(n%2 ==0){

           count1++;

           n= n/2;

       }

       if(count1 >=1){

           System.out.println(“2^” + count1);

       }

       for(int i=3; i<= Math.sqrt(n); i= i+2){

           int count2 =0;

           while(n% i == 0){

               count2++;

               n = n/i;

           }

          

           if(count2 >=1){

               System.out.println(” * ” + i + “^ “+ count2);

           }

       }

   }

}

————-

Expert Answer

 Hi, I have corrected your code: –

import java.util.Scanner;

public class Main {

public static boolean Isprime(int N) { // check if N is prime
boolean mark = true; // default

if (N == 1)
return false; // not a prime

for (int i = 2; i < N – 1; i++) {
if (N % i == 0) {
mark = false;
break;
}
}
return mark;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter N for #1 and #2 : “);
int N = sc.nextInt();

int count = 0;
int k = 0;
System.out.println(“N is : ” + N);
System.out.println(N + ” Primes Are: “);
while (true) {
if (count > 0 && Isprime(count)) {
System.out.println(count + ” “);
++k;
}
if (k == N)
break;
++count;
}

System.out.println(“2. primes not exceeding ” + N + ” : “);
for (int i = 1; i <= N; ++i) {
if (Isprime(i)) {
System.out.println(i + “”);
}
}

System.out.println(“Enter new N for question 3: “);
Scanner nsc = new Scanner(System.in);
int n = nsc.nextInt();
primefactors(n);
}

public static void primefactors(int n) {
int count1 = 0; // print the number of 2s divide in
int x = n;
while (n % 2 == 0) {
count1++;
n = n / 2;
}
if (count1 >= 1) {
System.out.print(“2^” + count1);
}
int limit = new Double(Math.ceil(Math.sqrt(n))+1).intValue();
int c = 0;
for (int i = 3; i <= limit; i = i + 1) {

int count2 = 0;
while (n % i == 0) {
count2++;
n = n / i;
}

if (count2 >= 1) {
if((c == 0 && count1 >=1) || c > 0) {
System.out.print(“*” + i + “^” + count2);
} else {
System.out.print(i + “^” + count2);
}

c++;
}

}
}

}

Output: –

Enter N for #1 and #2 :
10
N is : 10
10 Primes Are:
2
3
5
7
11
13
17
19
23
29
2. primes not exceeding 10 :
2
3
5
7
Enter new N for question 3:
252525
3^1*5^2*7^1*13^1*37^1

Still stressed from student homework?
Get quality assistance from academic writers!