Defines whether number is prime. - Java java.lang

Java examples for java.lang:Math Number

Description

Defines whether number is prime.

Demo Code

// Copyright (C) GridGain Systems, Inc. Licensed under GPLv3, http://www.gnu.org/licenses/gpl.html
//package com.java2s;

public class Main {
    /**//from w  ww. ja v a 2 s.c  o m
     * Defines whether number is prime.
     *
     * @param num Number.
     * @return {@code true} if number is prime, {@code false} if not.
     */
    static boolean isPrime(int num) {
        boolean prime = false;

        if (num > 0) {
            prime = true;

            for (int i = 2; i * i <= num; i++) {
                if (num % i == 0) {
                    prime = false;

                    break;
                }
            }
        }

        return prime;
    }
}

Related Tutorials