Wednesday 25 November 2015

How to Check number is prime or not


A number which is divided by one and itself is called prime number. For Example

7 is a prime number and
8 is not a prime number

package com.javaproficiency;

public class PrintPrime {

public static void main(String[] args) {
int i =101;
boolean flag = false;
for(int j=2 ; j <= i/2; j++){
if(i%j==0)
{
flag = true ;
break;
}
}
if(flag){
System.out.println(" number "+ i +" is not prime");
}else{
System.out.println(" number "+ i +" is  prime");
}

}

}


Output :

 number 101 is  prime

No comments:

Post a Comment