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
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 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); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
