Wednesday 25 November 2015

How to find largest element in an array with index and value ?

package com.javaproficiency;

public class LargestElement {
//How to find largest element in an array with index and value ?

public static void main(String[] args) {

int [] arr ={4,5,10,12,8,6};
int largest =0;
int index =0;
for(int i =0 ; i < arr.length; i++){
if( largest < arr[i]){
largest = arr[i];
index = i;
}
}
System.out.println(" largest element  ="+ largest + " index ="+index);

}

}

Output :

 largest element  =12 index =3

No comments:

Post a Comment