Q:

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

0

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

Expected Output:

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

Heights of the top three buildings:
45
25
24

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now