I was trying to create a program that printed all the prime numbers within a given limit. My program is printing out all of the numbers! See if you can figure out where I'm going wrong:
Code:import java.io.*; class PrimeCheck { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Limit --> "); int limit = Integer.parseInt(br.readLine()); System.out.println(""); System.out.print("Primes --> "); int count = 0; for (int i=2; i<=limit; i++) { for (int j=1; j<=i; j++) { if (j * i == i) { count++; } if (i * j == i) { count++; } if (count == 2) { System.out.print(i + " "); } count = 0; } } } }




Reply With Quote