# Java: Find heights of the top three building in descending order from eight given buildings

## Java Basic: Exercise-211 with Solution

Write a Java program to find the heights of the top three buildings in descending order from eight given buildings.

Input:

0 ≤ height of building (integer) ≤ 10,000

Visual Presentation:

Sample Solution:

Java Code:

``````import java.util.*;

public class Main {
public static void main(String[] args) {
// Creating a Scanner object for user input
Scanner sc = new Scanner(System.in);

// Array to store the heights of eight buildings
int[] t = new int[8];

// Prompting the user to input the heights of eight buildings
System.out.println("Input the heights of eight buildings:");
for (int i = 0; i < 8; i++) {
t[i] = sc.nextInt();
}

// Sorting the array of building heights in ascending order
Arrays.sort(t);

// Displaying the heights of the top three buildings in descending order
System.out.println("\nHeights of the top three buildings:");
for (int i = 7; i >= 5; i--) {
System.out.println(t[i]);
}
}
}
```
```

Sample Output:

```Input the heights of eight buildings:
25 19 23 45 18 23 24 19

Heights of the top three buildings:
45
25
24
```

Flowchart:

Java Code Editor: