It is a code for primality test. If you have a more efficient code or a clean code, please give me some advice.
import java.io.*;
class file name{
public static void main(String[]args) throws IOException
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter a positive integer");
String str = br.readLine();
int a =Integer.parseInt(str);
boolean sosu = true;
if(a == 1)sosu = false;
for(int b = 2; b*b <= a; b++){
if(a%b == 0){
sosu = false;
break;
}
}
if(sosu == true) System.out.println("\n"+a+"Is a prime number");
else System.out.println("\n"+a+"Is not a prime number");
}
}
Recommended Posts