Sunday, August 12, 2012

Java program to Compare elements of an array


import java.io.*;
class compelarray
{
int a[],big,small,n;
compelarray(int x)
{
n=x;
a=new int[n];
}
void readarray()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter elements of array: ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
}
void compare()
{
big=small=a[0];
for(int i=0;i<n;i++)
{if(big<a[i])
big=a[i];
if(small>a[i])
small=a[i];
}
System.out.println("The biggest element is : "+big);
System.out.println("The smallest element is : "+small);
}
public static void main(String[] args)throws IOException
{
int size;
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of the array : ");
size=Integer.parseInt(br2.readLine());
compelarray c= new compelarray(size);
c.readarray();
c.compare();
}
}

0 comments:

Post a Comment