Specification Write a Java application that prints the prime factorization of an
ID: 3770262 • Letter: S
Question
Specification
Write a Java application that prints the prime factorization of an input. Print "is prime" message if input is prime; otherwise, print the prime factorization of the input.
Prompt the user to enter a positive integer and read it from the standard input stream. Print an error message if the user does not enter a positive integer. The input must be read using the System.in object directly.
The following code snippet reads the standard input stream one character at a time until a newline encountered. Each entered character is printed to the standard output stream.
Docs.Oracle.com::class InputStream
Example Program Output
The inputs were 42, 37, 314, 3993, 101, 31046
Explanation / Answer
code:
import java.io.* ;
import java.util.*;
class abc {
public static void main(String args[])
{
Scanner inp = new Scanner(System.in);
int n;
while(inp.hasNext()){
n=inp.nextInt();
int i=2;
System.out.println(n);
int x=n;
Boolean prime=true;
while(i*i<=n){
if(n%i==0){
prime=false;
break;
}
else{
i++;
}
}
if(!prime){
n=x;
System.out.print(n);
System.out.print("=");
i=2;
while(i<=x){
if(n%i==0){
n=n/i;
if(n==1){
System.out.println(i);
break;
}
else{
System.out.print(i);
System.out.print("*");
}
}
else{
i++;
}
}
}
else{
System.out.print(x);
System.out.println(" is prime.");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.