Create a program that displays 20 whole random numbers from a range of -20 to 80 in a column and counts the number of negative numbers and their sum and the number of positive numbers and their sum and displays this data on the screen.

Made the first part, but with the calculation does not work.

<?php //начальное значение суммы чётных чисел $sum_even_numbers = 0; //начальное значение суммы нечётных чисел $sum_odd_numbers = 0; //количество чётных чисел $count_even_numbers = 0; //количество нечётных чисел $count_odd_numbers = 0; for ($i=1; $i<=20; $i++) { $number[$i] = rand(-20,80); echo "<br> $number[$i]"; if ($number[$i]<0) else ?> 
  • And what's the problem? You do not know how to add the two numbers in PHP? - Vladimir Martyanov

1 answer 1

If we are talking about parity, then:

 <?php $sum_even_numbers = 0; $sum_odd_numbers = 0; $count_even_numbers = 0; $count_odd_numbers = 0; for ($i=1; $i<=20; $i++) { $number[$i] = rand(-20,80); echo "<br> $number[$i]"; if ($number[$i]%2 = 0){ $count_even_numbers++; $sum_even_numbers += $number[$i]; } else{ $count_odd_numbers++; $sum_odd_numbers += $number[$i]; } } ?> 

If we talk about positive and negative, then:

 <?php $sum_minus = 0; $sum_plus = 0; $count_minus = 0; $count_plus = 0; for ($i=1; $i<=20; $i++) { $number[$i] = rand(-20,80); echo "<br> $number[$i]"; if ($number[$i]< 0){ $count_minus++; $sum_minus += $number[$i]; } else{ $count_plus++; $sum_plus += $number[$i]; } } ?>