fork download
  1. <?php
  2. /**
  3.  * We use an array to store all the numbers that can be divided by three
  4.  */
  5. $div_by_three = [];
  6.  
  7. /**
  8.  * fill the array with values that can be divided by three
  9.  */
  10. for($c=1; $c< 100; $c++) {
  11. if($c % 3 === 0) {
  12. $div_by_three[] = $c;
  13. }
  14. }
  15. /**
  16.  * Sum the array values into one number
  17.  */
  18. $sum = array_sum($div_by_three);
  19. echo "The sum of those dividable by three is $sum\n";
  20.  
  21. /**
  22.  * Divide the sum by array length
  23.  */
  24. $avg = $sum / count($div_by_three);
  25. echo "The average of those dividable by three is $avg\n";
  26.  
  27.  
Success #stdin #stdout 0.02s 23216KB
stdin
Standard input is empty
stdout
The sum of those dividable by three is 1683
The average of those dividable by three is 51