Java - determines if input number is prime

Description

determines if input number is prime

Demo

import java.util.Scanner;

public class IsPrime {

  public static void main(String args[]) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a number:");
    int number = keyboard.nextInt();
    keyboard.close();/*from ww w .j  av a 2  s.  co m*/

    boolean isPrime = true;

    for (int i = 2; i < (number / 2); i++) {
      if (number % i == 0) {
        isPrime = false;
        break;
      }
    }

    if (isPrime) {
      System.out.println("PRIME");
    }
    else {
      System.out.println("NOT PRIME");
    }
  }
}

Related Topic