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

Leave a Reply

Your email address will not be published. Required fields are marked *

48 − = 42