This is a simple java program to find the third highest/largest number in given Array. Let’s see the below example to find out the third largest number in an array.
Copy the below java program and execute it with the help of Java compiler to see the output. At the end of this tutorial, I have shared some execution results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class ThirdLargestInArray { public static int getThirdLargest(int[] a, int total){ int temp; for(int i = 0; i < total; i++) { for (int j = i + 1; j < total; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a[total-3]; } public static void main(String args[]){ int a[]={1,2,5,9,3,2}; int b[]={44,62,99,77,33,22,55}; System.out.println("Third Largest: "+getThirdLargest(a,6)); System.out.println("Third Largest: "+getThirdLargest(b,7)); }} |
Third Largest:5
Third Largest:62
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Java program, java program to find largest number, Java programming, Java programs, Java programs with examples, Java programs with output