Java Exercises: 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 heights of the top three building in descending order from eight given buildings.
Input:
0 ≤ height of building (integer) ≤ 10,000
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] t = new int[8];
System.out.println("Input the heights of eight buildings:");
for(int i = 0; i < 8; i++){
t[i] = sc.nextInt();
}
Arrays.sort(t);
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:
Contribute your code and comments through Disqus.
Previous: Write a Java program to match any single character (use ?) or any sequence of characters use *) including the empty. The matching should cover the entire input string.
Next: Write a Java program to compute the digit number of sum of two given integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework