public class PrintAllPrime {
public static void main(String args[]) {
int num = 22;
int count;
for (int i = 2; i <= num; i++) {
count = 2;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 2) {
System.out.print(i + " ");
}
}
}
}
Output:
2 3 5 7 11 13 17 19
public static void main(String args[]) {
int num = 22;
int count;
for (int i = 2; i <= num; i++) {
count = 2;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 2) {
System.out.print(i + " ");
}
}
}
}
Output:
2 3 5 7 11 13 17 19
No comments:
Post a Comment