Write a PHP script to calculate and display average temperature, five lowest and highest temperatures

Introduction

PHP is a server scripting language, and It is a powerful tool for making interactive and dynamic Web-pages. I have used WampServer 2.2 for following excercise..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. <?php
  2. $month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
  3. 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";
  4. $temp_array = explode(',', $month_temp);
  5. $tot_temp = 0;
  6. $temp_array_length = count($temp_array);
  7. foreach($temp_array as $temp)
  8. {
  9. $tot_temp += $temp;
  10. }
  11. $avg_high_temp = $tot_temp/$temp_array_length;
  12. echo "Average Temperature is : ".$avg_high_temp."
  13. ";
  14. sort($temp_array);
  15. echo " <br>List of five lowest temperatures :";
  16. for ($i=0; $i< 5; $i++)
  17. {
  18. echo $temp_array[$i].", ";
  19. }
  20. echo "<br>List of five highest temperatures :";
  21. for ($i=($temp_array_length-5); $i< ($temp_array_length); $i++)
  22. {
  23. echo $temp_array[$i].", ";
  24. }
  25. ?>

Result

python language techstudy.org

     





Related Topics




Popular Posts