Write a Java multithreaded program having 3 threads, 1st user input will be given at the command line. then 1st thread will calculate whether that user given number is palindrome or not.
import java.util.Scanner;
public class vinnu11 {
public static void main(String args[]) {
Scanner vinnu = new Scanner(System.in);
int a;
Thread t;
System.out.print("Enter the number : ");
a = vinnu.nextInt();
FirstThread firstThread = new FirstThread(a); //Creating an object of the first thread
SecondThread secondThread = new SecondThread(a); //Creating an object of the Second thread
firstThread.start(); //Starting the first thread
secondThread.start(); //Starting the second thread
try {
firstThread.join();
} catch (InterruptedException ex) {
}
try {
secondThread.join();
} catch (InterruptedException ex) {
}
if (firstThread.isAlive()) {
System.out.println("first thread is alive ");
} else {
System.out.println("first thread is not alive ");
}
if (secondThread.isAlive()) {
System.out.println("second thread is alive ");
} else {
System.out.println("second thread is not alive ");
}
factorial(a);
}
static void factorial(int a) {
int i, j, k;
k = a;
j = 1;
while (a > 0) {
j = j * a;
a--;
}
System.out.println("THREAD MAIN : Factorial of " + k + " = " + j);
}
}
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; /// modulos of a
y = (y * 10) + i; /// multiply 10 to y and adding i
a = a / 10; ///take example of 123
}
if (k == y) {
System.out.println("THREAD 1 : " + k + " == " + y);
System.out.println("THREAD 1 : " + "The given number is an palindrome number !!!");
} else {
System.out.println("THREAD 1 : " + "The given number is not a palindrome number !!");
}
}
}
class SecondThread extends Thread {
int a;
public SecondThread(int a) {
this.a = a;
}
@Override
public void run() {
int i, j, n;
point:
for (i = 2; i < a; i++) {
for (j = 2; j < i; j++) {
n = i % j;
if (n == 0) {
continue point;
}
}
System.out.println("THREAD 2 : " + i + " is an prime number ");
}
}
}
Write a Java multithreaded program having 3 threads, 1st user input will be given at the command line. then 1st thread will calculate whether that user given number is palindrome or not.
Reviewed by Unknown
on
01:45
Rating:
No comments: