Thursday, 4 August 2011

ARRAY 3


1. Create 3 arrays named A, P & N. Assign 15 numbers in array A. Shift all the positive numbers in array P and all the negative numbers in array N. Finally print the array P & N. import java.io.DataInputStream;
import java.io.IOException;
public class ques14
{
  public static void main()throws IOException
  {
    DataInputStream x=new DataInputStream(System.in);
    int a[]=new int[5];
    int p[]=new int[5];
    int n[]=new int[5];
    int i,k=0,l=0;
    for(i=0;i<5;i++)
    {
      System.out.print("enter a number: ");
      a[i]=Integer.parseInt(x.readLine());
    }
    for(i=0;i<5;i++)
    {
      if(a[i]>0 && a[i]%2==0)
      {
        p[k]=a[i];
        k++;
      }
    }
    for(i=0;i<5;i++)
    {
      if(a[i]<0 && a[i]%2!=0)
      {
        n[l]=a[i];
        l++;
      }
    }
    System.out.print("List of positive numbers");
    for(i=0;i<k;i++)
      System.out.println(p[i]);
    System.out.print("List of negative numbers");
    for(i=0;i<l;i++)
      System.out.println(n[i]);
  }
} 2. Accept numbers into an array of size 10. Then accept a number and search that number in array. If the number is present in array, then display the array element number where number is found. In case of multiple found display all the positions. Display a proper message if the number is not present in array. public class ques13
{
  public static void main(int number)
  {
    int i,j;
    boolean flag=false;
    int a[]={2,6,8,5,1,4,8,0,2,7};
    for(i=0;i<10;i++)
    {
      if(a[i]==number)
      {
        System.out.println("Match found in position:"+(i+1));
        flag=true;
      }
    }
    if(flag==false)
      System.out.println("Number not found in Array");
  }
} 3. WAP to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle. public class ques39
{
  public static void main()
  {
    int size=5;
    int a[][]=new int[size][size];
    int i,j;
    a[0][0]=1;
    // prepare the pascal triangle //
    for(i=1;i<size;i++)
    {
      for(j=0;j<size;j++)
      {
        if((i==j) || (j==0))
          a[i][j]=1;
        else
          a[i][j]=a[i-1][j-1]+a[i-1][j];
      }
    }
    // display the pascal triangle //
    for(i=0;i<size;i++)
    {
      for(j=1;j<=15-i;j++)
        System.out.print(" ");
      for(j=0;j<=i;j++)
        System.out.print(a[i][j]+" ");
      System.out.println();
    }
  }
} 4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort technique) import java.io.DataInputStream;
import java.io.IOException;
public class ques19
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String n[]=new String[5];
    int m[]=new int[5];
    int i,j,k;
    for(i=0;i<5;i++)
    {
      System.out.print("Enter Name : ");
      n[i]=stdin.readLine();
      System.out.print("Enter Comp. Sc. Marks : ");
      m[i]=Integer.parseInt(stdin.readLine());
    }
    // Sort the array in Descending order
    String x=null;
    for(i=0;i<4;i++)
    {
      for(j=i+1;j<5;j++)
      {
        if(m[i]<m[j])
        {
          k=m[i]; m[i]=m[j]; m[j]=k;
          x=n[i]; n[i]=n[j]; n[j]=x;
        }
      }
    }
    // display Merit List
    System.out.println("Name \t\t Marks");
    for(i=0;i<5;i++)
      System.out.println(n[i]+"\t\t"+m[i]);
  } // end of method
} 5. WAP to assign numbers into a 4X3 matrix. Display the original matrix and display the matrix in transpose form. // Transpose a Matrix //
public class ques32
{
  public static void main()
  {
    int a[][]={{4,5,8,1},{2,8,7,4},{3,4,5,6}};
    int i,j;
    for(i=0;i<3;i++)
    {
      for(j=0;j<4;j++)
        System.out.print(a[i][j]+" ");
      System.out.println();
    }
    System.out.println();
    for(i=0;i<4;i++)
    {
      for(j=0;j<3;j++)
        System.out.print(a[j][i]+" ");
      System.out.println();
    }
  }
}

No comments:

Post a Comment