Quick actions

cmd+k|ctrl+k

Navigation

Languages

Test for prime numbers

Snippet info

Language

Java

Visibility

public

Author

cafebabe1991

Created

2016-05-11T11:38:54Z

Updated

2016-05-11T11:40:51Z

class Main {
    public static void main(String[] args) {
        //tells whether a given no. is prime or not ?
        boolean flag = isPrime(4);
        System.out.println("4 is prime ?" +flag);
        flag = isPrime(432);
        System.out.println("43 is prime ?" + flag);
    }
    
    static boolean isPrime(int N) {
        boolean check = true;
        if (N == 1 || N == 0) {
            check = false;
        }
        else {
            //run till the square root of the number
            for (int i =2; i<= Math.sqrt(N); i++) {
                if(N%i == 0) {
                    check = false;
                    break;
                }
            }
        }
        return check;
    }
}
INFO