This simple example shows you how to find the index of largest number in an array using simple for loop
Step 1:
Assign array value
Assume largest number as array’s first value and its index as 0
Step 2:
Iterate array using a for loop.
Step 3:
Check max value is smaller than array current value, if true reassign the max value and its index position
Program
public class LargestNumberIndex { public static void main(String[] args) { int a[] = new int[] { 12, 44, 23, 56, 23, 78, 13 }; int max = a[0]; int index = 0; for (int i = 0; i < a.length; i++) { if (max < a[i]) { max = a[i]; index = i; } } System.out.println("Index position of Maximum value in an array is : " + index); } }
Output
Index position of Maximum value in an array is : 5