-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem49.java
51 lines (42 loc) · 987 Bytes
/
Problem49.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package dimikOJ;
import java.util.Scanner;
public class Problem49 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while (T < 1 || T > 10) {
T = input.nextInt();
}
input.nextLine();
int[] n = new int[T];
for (int i = 0; i < n.length; i++) {
int temp = input.nextInt();
while (temp < 2 || temp > 1000000000) {
temp = input.nextInt();
}
n[i] = temp;
}
input.close();
for (int i = 0; i < n.length; i++) {
if (isPrime(n[i])) {
System.out.println(n[i] + " is a prime");
} else {
System.out.println(n[i] + " is not a prime");
}
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
}