You are viewing a single comment's thread. Return to all comments →
The problem itself has wrong Sample Output as per Sample Input. The correct program for this Question is this: (Solved in Java 8)
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { new Prime().checkPrime(scan.nextInt()); } scan.close(); } } class Prime { ArrayList<Integer> list = new ArrayList<>(); void checkPrime(int n) { if (n > 1) { for (int i=2; i<=n; i++) { if (i == 2) list.add(i); else if (i == 3) list.add(i); else { int count = 0; for (int j=0; j<list.size(); j++) { if (i % list.get(j) == 0) { break; } else { count++; } } if (count == list.size()) list.add(i); } } } for (int i=0; i<list.size(); i++) { if (i != list.size()-1) System.out.print(list.get(i)+" "); else System.out.println(list.get(i)); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Prime Checker
You are viewing a single comment's thread. Return to all comments →
The problem itself has wrong Sample Output as per Sample Input. The correct program for this Question is this: (Solved in Java 8)