How to check number as Prime

Published 2015-11-19
How to check number is prime or not

All Comments (2)
  • Program Code: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PrimeCheck { public static void main(String[] args)throws IOException { int num=0; int count=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//bufferedreader to get number from keyboard System.out.println("Enter a number ");//ask the number num = Integer.parseInt(br.readLine());//get the number change to int as in java all are String for(int i=2;i<=num-1;i++){//loop to check all factors if(num%i==0) count++;//if divisible increment count break; } if(count>0){ System.out.println("number is not prime");//check for count if >0 then not prime as prime has only1 and itself as factor } else { System.out.println("Number is prime"); } }//main method ends here }//class ends here