Java - Check if a number is prime number

Description

Check if a number is prime number

Demo

public class Main {

  static boolean isPrime(int number) {
    for (int i = 2; i <= Math.sqrt(number); i++) {
      if (number % i == 0) {
        return false;
      }/*from   w ww. j a  v  a 2  s  . co  m*/
    }
    return true;
  }

  public static void main(String[] args) {
    int number = 89;
    if (number >= 2 && isPrime(number)) {
      System.out.println("Prime");
    } else {
      System.out.println("Not prime");
    }
  }
}

Related Topic