Java while loop statement calculate prime number

Question

We would like to find the first 100 prime number

public class Main {
 
    public static void main(String[] argv) {
        int quantity = 100;
        int numPrimes = 0;
        // candidate: the number that might be prime
        int candidate = 2;
        //your code here
    }/*from   w  w w . j ava  2 s  .  c om*/
 
    public static boolean isPrime(int checkNumber) {
        
        //your code here
    }

 
}


 
public class Main {
 
    public static void main(String[] argv) {
        int quantity = 100;
        int numPrimes = 0;
        // candidate: the number that might be prime
        int candidate = 2;
        
        while (numPrimes < quantity) {
            if (isPrime(candidate)) {
                System.out.println(candidate);
                numPrimes++;
            }
            candidate++;
        }
    }
 
    public static boolean isPrime(int checkNumber) {
        double root = Math.sqrt(checkNumber);
        for (int i = 2; i <= root; i++) {
            if (checkNumber % i == 0) {
                return false;
            }
        }
        return true;
    }

 
}



PreviousNext

Related