Answer to Question #289301 in Java | JSP | JSF for Ron

Question #289301

Question can be found here http://ntci.on.ca/compsci/java/ch8/8_7.html

Its question 11, please don't use import.java Please use arrays and do this assignment simply, besides arrays you can make it simple. I would highly appreciate your free help.


1
Expert's answer
2022-01-21T01:46:04-0500
public class Main {
    public static void main(String[] args) {
        int upperBound = 1000;
        boolean[] primes = new boolean[upperBound + 1];
        for (int i = 2; i < primes.length; i++) {
            for (int j = i * i; j < primes.length; j += i) {
                primes[j] = true;
            }
        }
        int counter = 0;
        for (int i = 2; i < primes.length; i++) {
            if (!primes[i]) {
                System.out.printf("%10d", i);
                counter++;
            }
            if (counter == 8) {
                counter = 0;
                System.out.println();
            }
        }
    }
}

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