A)
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.
Answer :
import java.io.*;
public class Slip29A {
public static void main(String args[]) {
try {
DataInputStream br = new DataInputStream(System.in);
System.out.println("********* java
program using multithreading *********");
System.out.print("Enter Number :
");
int n = Integer.parseInt(br.readLine());
OddNumber a1 = new OddNumber(n);
a1.start();
PrimeNumber a2 = new PrimeNumber(n);
a2.start();
} catch (Exception e) {}
}
}
class OddNumber extends Thread {
int odd;
OddNumber(int n) {
odd = n;
}
public void run() {
System.out.println("****** Odd Numbers *******");
for (int i = 1; i <= odd; i++) {
if (i % 2 != 0) {
System.out.print(i + " | ");
}
}
System.out.println();
}
}
class PrimeNumber extends Thread {
int prime;
PrimeNumber(int n){
prime = n;
}
public void run() {
System.out.println("****** Prime Numbers *******");
for (int i = 2; i <= prime; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime == true)
System.out.print(i + " | ");
}
}
}
B)
Write a SERVLET program to change inactive time interval of session.
Answer :
Output :
No comments:
Post a Comment