mercredi 2 octobre 2019

the output of 2nd for loop

i have this code here and I am having a really hard time to figure out that isn't when i == 6 that would result in two results in the second loop when 6%2 = 0 and 6%3 = 0 and 6%4 =2. so how the compiler decides which if 6 is a prime number or not

    public static void main(String[] args) {
    for (int i = 2; i <100 ; i++) {
        if (isPrime (i)) 
            System.out.println(i);
    }

}
        private static  boolean isPrime(int n) {    

        for (int i =2; i< n; i++)
            if (n%i == 0)
            return false ;
            return true;
        }
}

Aucun commentaire:

Enregistrer un commentaire