Program which finds the smallest and largest element of the array and print its position also.

import java.io.*;
class test
{
public static void main(String[] asd)throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of values you wamma enter.");
String num=obj.readLine();
int n=Integer.parseInt(num);

int m[]= new int[n];
System.out.println("Enter the values of the array not more then "+n+".");
for(int i=0;i<n;i++)
{
num=obj.readLine();
m[i]=Integer.parseInt(num);
}
System.out.println("ARRAY-->");
for(int i=0;i<n;i++)
System.out.print(m[i]+" ");
int max=m[0],min=m[0];
int p=0;
for(int i=0;i<n;i++)
{
if(m[i]>max)
{
max=m[i];
p=i;
}
}
System.out.println("The maximum element is "+max+" at position"+p+".");
for(int i=0;i<n;i++)
{
if(m[i]<min)
{
min=m[i];
p=i;
}
}
System.out.println("The Minumum element is "+min+" at position"+p+".");
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s