-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqb150.java
More file actions
65 lines (52 loc) · 1.47 KB
/
Copy pathqb150.java
File metadata and controls
65 lines (52 loc) · 1.47 KB
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
/* QB 150:
Write a program to create two threads, one thread will check whether given number is
prime or not and second thread will print prime numbers between 0 to 100
*/
class MyThread1 extends Thread {
int n;
MyThread1(int n) {
this.n = n;
}
public void run() {
int count = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 2) {
System.out.println(Thread.currentThread().getName() + " Entered number " + n + " is Prime number");
} else {
System.out.println(Thread.currentThread().getName() + " Entered number " + n + "is not Prime number");
}
}
}
class MyThread2 extends Thread {
int n;
MyThread2(int n) {
this.n = n;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " Prime numbers between 0 to " + n + " are: ");
int count = 0;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 2) {
System.out.print(i + " ");
}
}
}
}
class QB150 {
public static void main(String[] args) throws InterruptedException {
MyThread1 t1 = new MyThread1(61);
t1.start();
t1.join();
MyThread2 t2 = new MyThread2(100);
t2.start();
}
}