public class SieveOfErastothenes { public SieveOfErastothenes(int maxNumber) { int[] array; array = new int[maxNumber + 1]; // initializing array to 2...maxNumber for (int i=2; i<=maxNumber; i++) { array[i] = i; } // computing the primes by removing multiple of primes for (int i=2; i<=(int)Math.sqrt(maxNumber); i++) { for (int j= 2*i; j <= maxNumber; j+=i) { array[j] = 0; } } // printing the primes for (int i=2; i<=maxNumber; i++) { if (array[i] != 0) { System.out.println(array[i]); } } } public static void main(String[] args) { new SieveOfErastothenes(100); } }