Create a new Class with name Chk_prime :
Output:(Click on image to enlarge)
import java.util.Scanner;
public class Chk_prime {
public static void main(String[] args) {
// TODO Auto-generated method stub
int rem=0,i=2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it is prime : ");
int num = sc.nextInt();
System.out.println("You entered : "+num);
if(num==2){
System.out.print(num+" is even prime");
}
else{
while(i<num){
rem = num%i;
if(rem==0){
System.out.print(num+" is not prime");
break;}
else
i++;
}
if(rem!=0)
System.out.print(num+" is prime");
}
}
}
public class Chk_prime {
public static void main(String[] args) {
// TODO Auto-generated method stub
int rem=0,i=2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it is prime : ");
int num = sc.nextInt();
System.out.println("You entered : "+num);
if(num==2){
System.out.print(num+" is even prime");
}
else{
while(i<num){
rem = num%i;
if(rem==0){
System.out.print(num+" is not prime");
break;}
else
i++;
}
if(rem!=0)
System.out.print(num+" is prime");
}
}
}
(Comment if you have any doubts)