display prime numbers from 5 to 5000 using the while loop, for loop and do while loop
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);
}
}
Comments
Leave a comment