Monday 9 June 2014

Program to find largest and smallest element of an array

import javax.swing.JOptionPane;
public class largestandsmallestamonganarray
 {
public static void main(String[] args)
{ int n;
n=Integer.parseInt(JOptionPane.showInputDialog("enter size "));
int []a= new int[n];
System.out.print("enter array elements");
for(int i=0; i<n; i++)
{
a[i]=Integer.parseInt(JOptionPane.showInputDialog(" "));
}
int max=a[0], min=a[0];;
for(int i=0; i<n; i++)
{
if(a[i]>max)
max=a[i];
}

for(int i=0; i<n; i++)
{
if(a[i]<min)
min=a[i];
}
System.out.print("\nlargest is " +max);
System.out.print("\nsmalest is "+min);
}


}
output:
enter size 5
enter array elements 1
6
8
4
3
largest is 6
smalest is 1

No comments:

Post a Comment

SAY HELLO!!