-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPrimeNumbers.java
More file actions
36 lines (33 loc) · 791 Bytes
/
Copy pathNPrimeNumbers.java
File metadata and controls
36 lines (33 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public class NPrimeNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int j = 1, i = 1;
while (i <= 10) {
if (isPrime(j)) {
System.out.print(j + " ");
i++;
}
j++;
}
scan.close();
}
public static boolean isPrime(int num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
} else {
for (int i = 3; i < num; i = i + 2) {
if (num % i == 0) {
return false;
}
}
}
return true;
}
}