0% found this document useful (0 votes)
20 views2 pages

M 7

The document describes a Java program that uses multithreading to display odd numbers and prime numbers between 1 and a user-input number n. The program creates a MyThread class that implements Runnable and contains methods to print odd numbers and check for prime numbers in parallel threads.

Uploaded by

rohidas badhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

M 7

The document describes a Java program that uses multithreading to display odd numbers and prime numbers between 1 and a user-input number n. The program creates a MyThread class that implements Runnable and contains methods to print odd numbers and check for prime numbers in parallel threads.

Uploaded by

rohidas badhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// Write a java program using multithreading for the following: 1.

Display all
// the odd numbers between 1 to n. 2. Display all the prime numbers between 1
// to n

import java.io.*;

@SuppressWarnings("ALL")
class MyThread implements Runnable {
int i, n;
Thread t1;

MyThread() {
t1 = new Thread(this, "Odd");
t1.start();
}

public void run() {


try {
Thread.sleep(1000);
for (i = 1; i <= n; i = i + 2) {
Thread.sleep(1000);
System.out.println(i);
}
} catch (Exception obj) {
System.out.println(obj);
}
}

public void run2(){


try {

int count;
Thread.sleep(1000);
for (int i = 1; i <= n; i++) {
count = 0;
Thread.sleep(1000);

for (int j = 2; j <= i / 2; j++) {


if (i % j == 0) {
count++;
break;
}
}

if (count == 0) {
System.out.println(i);
}
}
} catch(Exception obj) {
System.out.println(obj);
}
}
}

class m7 {
public static void main(String[] args) throws Exception {
MyThread obj = new MyThread();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Range Value");
obj.n = Integer.parseInt(br.readLine());

System.out.println("Odd Number : ");


obj.run();

System.out.println("Prime Number : ");


obj.run2();
}
}

You might also like