How to Find the Third Greatest Number in Array in Java

This is the interesting java programming exercise for beginners. The java program will displays the 3rd largest numbers in an Array in java e.g. Given array is [14 , 15 , 55 , 77 , 9] its third greatest will be [15].

Source Code  

package newpackage.more;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class thirdLargest {

/**
     * @param args the command line arguments
     */
public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.println("Enter values: ");
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = in.nextInt();
}
Arrays.sort(array);
System.out.println("Third largest" + array[array.length-3]);
}
}

Output of the Program

Find the Third Greatest Number in Array in Java

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: