Thursday, 4 August 2011

ARRAY 5


1. Assign/Store 10 numbers into an array and then find the Highest and Lowest value in the array with their position. public class MaxMinPos
{
  public static void main()
  {
    int i;
    int a[]={3,7,2,4,1,6,7,10,9,8};
    int max=a[0],min=a[0];
    int maxPos=0,minPos=0;
    for(i=0;i<10;i++)
    {
      if(a[i]>max)
      {
        max=a[i];
        maxPos=i;
      }
      if(a[i]<min)
      {
        min=a[i];
        minPos=i;
      }
    }
    System.out.println("Maximum No. = "+max);
    System.out.println("Position = "+maxPos);
    System.out.println("Minimum No. = "+min);
    System.out.println("Position = "+minPos);
  }
} 2. Assign numbers into two array A & B of size 5. Then find common element between arrays and transfer them in another array. public class ques8
{
  public static void main()
  {
    int i,j,x=0;
    int a[]={1,2,3,4,5};
    int b[]={3,4,5,6,7};
    int c[]=new int[5];
    for(i=0;i<5;i++)
    {
      for(j=0;j<5;j++)
      {
        if(a[i]==b[j])
        {
          c[x]=a[i];
          x++;
        }
      }
    }
    for(i=0;i<x;i++)
      System.out.println(c[i]);
  }
}
3. Write a program that accept 10 integers from user in such a way that no negative and zero accepted to array. import java.io.*;
public class nonNegative
{
  public static void arr() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    int n[]=new int[10];
    int i=0;
    do
    {
      System.out.print("Enter a number : ");
      int a=Integer.parseInt(stdin.readLine());
      if(a>0)
      {
        n[i]=a;
        i++;
      }
    }while(i<10);
    for(i=0;i<10;i++)
      System.out.println("The number is : " + n[i]);
  }
}
4. Create an array of size 10 then accept 9 numbers into it. Then accept another number and its position where you want to insert it. Insert the given number in given position then Display the array. import java.io.DataInputStream;
import java.io.IOException;
public class ques10
{
  public static void main()throws IOException
  {
    DataInputStream x=new DataInputStream(System.in);
    int a[]=new int[10];
    int i,p,n;
    for(i=0;i<9;i++)
    {
      System.out.print("enter a number: ");
      a[i]=Integer.parseInt(x.readLine());
    }
    System.out.print("Enter the number: ");
    n=Integer.parseInt(x.readLine());
    System.out.print("Enter position : ");
    p=Integer.parseInt(x.readLine());
    for(i=9;i>=p;i--)
      a[i]=a[i-1];
    a[p-1]=n;
    for(i=0;i<10;i++)
      System.out.println(a[i]);
  }
}

No comments:

Post a Comment