Program to create two threads, one for Prime number and other for Non-prime number

This is a simple program which prints prime and non-prime numbers from 1 to 10. 


PROGRAM
class Prime extends Thread {
 
 public void run() {
  
  for(int i=1; i<=10; i++) {
   if(i==2 || i==3 || i==5 || i==7)
    System.out.println("Prime no. : " + i);
  }
 }
}

class NonPrime extends Thread {
 
 public void run() {
  
  for(int i=1; i<=10; i++) {
   if(i==4 || i==6 || i==8 || i==9 || i==10)
    System.out.println("Non-Prime no. : " + i);
  }
 }
}

public class PrimeNotPrimeThread {

 public static void main(String[] args) {
  
  new Prime().start();
  new NonPrime().start();
 }
}
OUTPUT
C:\>javac PrimeNotPrimeThread.java
C:\>java PrimeNotPrimeThread
Prime no. : 2
Prime no. : 3
Prime no. : 5
Prime no. : 7
Non-Prime no. : 4
Non-Prime no. : 6
Non-Prime no. : 8
Non-Prime no. : 9
Non-Prime no. : 10

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative