Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Write a Program to print all Prime numbers in an array of ‘n’ elements. (use command line arguments)

Write a Program to print all Prime numbers in an array of ‘n’ elements. (use command line arguments)

Become More Then A coder | Learn & Start Coding Now.

1.Write a Program to print all Prime numbers in an array of ‘n’ elements.

-
import java.util.Scanner;
public class PrimeNumbers{
public static void main (String[] args){
    int[] array = new int [5];
    Scanner in = new Scanner (System.in);

    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<5; i++)
    {
        array[i] = in.nextInt();
    }
Elements in the array are looped one by one using for loop.
    for(int i=0; i<array.length; i++){
        boolean isPrime = true;

Using for loop and if condition, prime numbers are identified from all the numbers that are entered in the array.
        for (int j=2; j<i; j++){

            if(i%j==0){
                isPrime = false;
                break;
            }
        }
     
All the prime numbers or the numbers that satisfy the if condition are displayed as output. 
        if(isPrime)

            System.out.println(i + " are the prime numbers in the array ");
    }
}
}

2.Define an abstract class Staff with protected members id and name. Define a parameterized

constructor. Define one subclass OfficeStaff with member department. Create n objects of

OfficeStaff and display all details.

-
import java.io.IOException;
import java.io.InputStreamReader;
abstract class Staff{
String name,address;
}
class FullTimeStaff extends Staff{
String department;
double salary;
public void accept() throws IOException{
System.out.println("Enter the name, address, department and salary: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine();
address=br.readLine();
department=br.readLine();
salary=Double.parseDouble(br.readLine());
}
public void display(){

System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("Department: "+department);
System.out.println("Salary: "+salary);
System.out.println("----------------------");
}
}
class PartTimeStaff extends Staff{
int hours, rate;
public void accept() throws IOException{
System.out.println("Enter the name, address, No of working hours and rate per hour: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine();
address=br.readLine();
hours=Integer.parseInt(br.readLine());
rate=Integer.parseInt(br.readLine());
}
public void display(){
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("No of Working Hours: "+hours);
System.out.println("Rate per hour: "+rate);
System.out.println("----------------------");
}
}
public class sb1 {
public static void main(String [] args) throws IOException{
int i;
System.out.println("Select Any One: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1.Full Time Staff");
System.out.println("2.Part Time Satff");
int ch=Integer.parseInt(br.readLine());
switch(ch){
case 1:
System.out.println("Enter the number of Full Time Staff: ");
int n=Integer.parseInt(br.readLine());
FullTimeStaff [] l=new FullTimeStaff[n];
for(i=0;i<n;i++){
l[i]=new FullTimeStaff();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
System.out.println("Enter the number of Part Time Staff: ");
int m=Integer.parseInt(br.readLine());
PartTimeStaff [] h=new PartTimeStaff[m];
for(i=0;i<m;i++){
h[i]=new PartTimeStaff();

h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
}

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.