You are on page 1of 2

CSE 1007

1. Write a Java multithreaded program having 2 threads, first thread should


check the user given number is palindrome or not and the second thread
to print the prime numbers. Create the threads by extending the Thread
class
CODE:-
import java.util.Scanner;
import java.lang.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a;
Thread t;
System.out.print("Enter the number : ");
a = sc.nextInt();
FirstThread firstThread = new FirstThread(a);
SecondThread secondThread = new SecondThread();
firstThread.start();
secondThread.start();
try {
firstThread.join();
} catch (InterruptedException ex) {
}
try {
secondThread.join();
} catch (InterruptedException ex) {
}
}
}
class FirstThread extends Thread {
private int a;
public FirstThread(int a) {
this.a = a;
}
public void run() {
int i, j, y, x, k;
k = a;
y = 0;
i = 0;
while (a > 0) {
i = a % 10;
y = (y * 10) + i;
a = a / 10;
}
if (k == y) {System.out.println("THREAD 1 : " + k + " == " + y);
System.out.println("THREAD 1 : " + "The given number is a palindrome number !!!");
} else {
System.out.println("THREAD 1 : " + "The given number is not a palindrome number !!");
}
}}
class SecondThread extends Thread {
public void run() {
int i =0;
int num =0;
String primeNumbers = "";
int n=50;
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("THREAD 2: Prime numbers from 1 to 50 are : ");
System.out.println(primeNumbers);
}
}

You might also like