Answer to Question #296318 in Java | JSP | JSF for Jones

Question #296318

display prime numbers from 5 to 5000 using the while loop, for loop and do while loop


1
Expert's answer
2022-02-11T00:27:36-0500
public class Main {
    public static void main(String[] args) {
        boolean[] primes = new boolean[5001];
        for (int i = 2; i < primes.length; i++) {
            for (int j = i * i; j < primes.length; j += i) {
                primes[j] = true;
            }
        }
        for (int i = 5; i < 5001; i++) {
            if (!primes[i]) {
                System.out.println(i);
            }
        }
        int i = 5;
        while (i < 5001) {
            if (!primes[i]) {
                System.out.println(i);
            }
            i++;
        }
        i = 5;
        do {
            if (!primes[i]) {
                System.out.println(i);
            }
            i++;
        } while (i < 5001);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS