Write a Java program to check if number is prime or not.

A prime number is a whole number greater than 1 whose only factors are 1 and itself. Below is the program to check if the number is prime or not:

package testjava.javatest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PrimeOrNot
{
	public static void main(String args[]) throws NumberFormatException, IOException
	{
		System.out.print("Enter a number to check if prime or not: ");
   	 
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	int number = Integer.parseInt(br.readLine());
    	
    	boolean checkPrime = isPrime(number);
    	
    	if(checkPrime)
    		System.out.println(number +" is a prime number.");
    	else
    		System.out.println(number +" is not a prime number.");
	}	
	
	public static boolean isPrime(int number) 
	{
		// No need to check till N-1, checking till square root of N is enough. 
		int sqrt = (int) Math.sqrt(number) + 1;
        
        for (int i = 2; i < sqrt; i++) 
        {
            if (number % i == 0) 
            {
                return false;
            }
        }
        return true;
    }
}