Q:

Finding whether number 4 is prime or not?

Here is my program, inside for loop Math.sqrt(n) - I think here is error.
import java.util.Scanner;

public class PrimeNoCheck {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.print("Enter a number : ");

        int n = s.nextInt();

        if (isPrime(n)) {
            System.out.println(n + " is a prime number");
        } else {
            System.out.println(n + " is not a prime number");
        }

    }

    public static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }

        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }

        return true;
    }
}

A:

the algorithm is: 
To check if n is a prime number, you have to try to divide it by all the numbers, from 2 (1 is useless), to sqrt(n), included.
You don't have to check after sqrt(n), because, if n can be divided by X,
greater than sqrt(n), it also can be divided by Y (n/X), lesser than sqrt(n), or by Z, lesser than Y, if Y is not prime.
The error in you code is that you stop just before sqrt(n).
With your code 4, 9, 25, ... are seen as prime, and every  prime ^2 also.
just Replace that:  <= instead of <
for (int i = 2; i <= Math.sqrt(n); i++) {

