﻿ PHP Exercise: Find heights of the top three building in descending order from eight given buildings - w3resource

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

## PHP: Exercise-46 with Solution

Write a PHP 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:

PHP Code:

``````<?php
// Create an empty array to store building heights
\$heights = array();

// Read input from STDIN until end-of-file
while (\$line = fgets(STDIN)) {
// Remove newline characters and convert to integer, then push to the heights array
rtrim(\$line, "\n");
array_push(\$heights, (int)\$line);
}

// Sort the heights array in descending order
rsort(\$heights);

// Print the top three building heights
print("Heights of the top three buildings:\n");
echo \$heights[0]."\n"; // Print the height of the tallest building
echo \$heights[1]."\n"; // Print the height of the second tallest building
echo \$heights[2]."\n"; // Print the height of the third tallest building

?>
```
```

Sample Output:

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

Flowchart:

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿