You are on page 1of 1

//printing Largest & Second Largest of an integer array

public class LargestSecondLargest{

static int LargeSecondLargest( int []a)


{
int largest= a[0];
int secLargest=a[0];

for( int i=1;i<a.length;i++){


if( a[i]> largest)
secLargest=largest;
largest= a[i];
elese if(a[i]>secLargest)
secLargest=a[i];
}

//main method

public static void main(String[] args) {


//creating an integer array of size 10
int [] arr= new int[10];

LargeSecondLargest(arr);
System.out.println(" the largest element of the array is:"+
largest);

System.out.println(" the second Largest element of the array


is:"+ secLargest);

You might also like