-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeChecker.java
70 lines (64 loc) · 1.53 KB
/
PrimeChecker.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
class Thread1 implements Runnable{
Thread t;
Thread1() {
t = new Thread(this);
t.start();
}
public void run() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i< 10; i++) {
System.out.println("Enter a number: ");
int n = sc.nextInt();
Thread2 ob = new Thread2(n);
try {
ob.t1.join();
}
catch(InterruptedException ie) {
System.out.println(ie);
}
}
sc.close();
}
}
class Thread2 implements Runnable{
int n;
Thread t1;
Thread2(int n) {
this.n = n;
t1 = new Thread(this);
t1.start();
}
public void run() {
boolean isPrime = true;
if (n <= 1) {
isPrime = false;
}
else {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(n + " PRIME");
}
else {
System.out.println(n + " NOT PRIME");
}
}
}
class PrimeChecker {
public static void main(String args[]) {
Thread1 ob = new Thread1();
try {
ob.t.join();
}
catch(InterruptedException ie) {
System.out.println(ie);
}
System.out.println("main thread exiting...");
}
}