Thursday 4 August 2011

ARRAY 4


1. WAP to accept ten elements in an array and transfer them in reverse form in a different array. import java.io.*;
public class arrReverse
{
  public static void main() throws IOException
  {
    DataInputStream in = new DataInputStream(System.in);
    int n[]=new int[10];
    int r[]=new int[10];
    for(int i=0;i<10;i++)
    {
      System.out.print("Enter a number : ");
      n[i]=Integer.parseInt(in.readLine());
    }
    for(int i=0,j=9;i<10;i++,j--)
    {
      r[i]=n[j];
    }
    for(int i=0;i<10;i++)
    {
      System.out.println(r[i]);
    }
  }
} 2. WAP to accept names of the 10 politicians and their party in two separate arrays. Ask the users to enter the name of the party and Print all the names of the politicians who belong to that party. import java.io.*;
public class ques23
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String party[]=new String[10];
    String poli[]=new String[10];
    String search;
    boolean flag=false;
    for(int i=0;i<10;i++)
    {
      System.out.print("Enter Party Name : ");
      party[i]=stdin.readLine();
      System.out.print("Enter Politician Name : ");
      poli[i]=stdin.readLine();
    }
    System.out.print("Enter Searching Party Name : ");
    search=stdin.readLine();
    System.out.println("Name of Politician's");
    for(int i=0;i<5;i++)
      if(search.compareTo(party[i])==0)
        System.out.println(poli[i]);
  }
} 3. WAP to input the names and computer marks of 50 students in arrays then Calculate and display:
(i) The subject average marks.
(ii) The highest mark and the name of the student. import java.io.*;
class highMarks
{
  public static void main()throws IOException
  {
    InputStreamReader inp=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(inp);
    int marks[]=new int[50];
    String name[]=new String[50];
    String HighName;
    int HighMarks,total=0;
    for(int i=0;i<50;i++)
    {
      System.out.println("Enter Name and Marks");
      name[i]=in.readLine();
      marks[i]=Integer.parseInt(in.readLine());
    }
    System.out.println("Enter name and marks");
    HighName=name[0];
    HighMarks=marks[0];
    total=HighMarks;
    for(int i=1;i<50;i++)
    {
      if(marks[i]>HighMarks)
      {
        HighMarks=marks[i];
        HighName=name[i];
      }
      total=total+marks[i];
    }
    double avg=total/50.0;
    System.out.println("Subject average marks is"+avg);
    System.out.println("Highest marks is "+HighMarks+"and the scorer is"+HighName);
  }
} [

No comments:

Post a Comment