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.
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();
}
}
}
}
Comments
Leave a comment