Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3845328 • Letter: T

Question

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

           }

       }

   }

}

Explanation / Answer

Your code works fine, for the part three no need to declare a new Scanner object, we can use the previously declared scanner object.

here is the bit modified code, i will have parameters as 8 and 72

import java.util.*;
import java.lang.*;
import java.io.*;


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: ");
int n = sc.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.print("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);
}
}
}
}

Output:

Enter N for #1 and #2 :
N is : 8
8 Primes Are:
2
3
5
7
11
13
17
19
2. primes not exceeding 8 :
2
3
5
7
Enter new N for question 3: 72
2^3 * 3^ 2

Hope this helps.