java program to find the second largest number in an Array


java program to find the second largest number in an Array.

 

/*
 *@ CopyRight : 2016
 *@ Author       : RAJEEVKUMAR
 *@ Date      : 16-Mar-2018
 *@ File Name : SecondLargest.java
 *@ Package   : codding.time
 *
 */
package codding.time;

public class SecondLargest {
    public static void main(String args[]) {
        int a[] = { 2, 3, 4, 5, 2, 3, 5, 6, 7 };

        int secondLargest = 0;

        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] < a[j]) {
                    secondLargest = a[i];
                    a[i] = a[j];
                    a[j] = secondLargest;
                }
            }
        }
        System.out.println(a[1]);
    }
}

Related Articles

post a comment