Find the index of the largest number in an array


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


Related example

        Java program to find largest number in an array
        Java program to find second largest number in an array
        Java program to find largest and smallest number in an array
        Java program to find largest and second largest number in an array
        Java program to find second smallest number in an array
        Java program to find index of max value in array java
        Java program to find index of smallest element in array java


Related Post

Comments


©candidjava.com