How to calculate the Quantity of even numbers of this vector?

-3

Create a vector with 7 integers. Print the largest and smallest, without ordering, the percentage of even numbers. Only that percentage is missing.

                                         

         echo "Maior valor: ".max($num);
         echo "</br> Menor valor: ".min($num)."</br>";

         foreach ($num as $numero){
                echo "$numero ";

         }


    ?>
</body>

    
asked by anonymous 14.06.2018 / 00:09

1 answer

0

Make sure the rest of the division of each number by 2 equals 0. If it is, add it to a counter.

<?php
// Porcentagem de pares
$array = Array(1, 2, 3, 4, 5, 6);
$contador = 0; // Responsavel por contar o numero de pares
foreach($array as $numero) {
    if($numero % 2 == 0) {
        $contador++;
    }
}
$porcentagemPares = ($contador/6)*100; //Divide o numero de pares pelo numero total de elementos e multiplica por 100
echo "Porcentagem de pares " .$porcentagemPares. "%";

Example working: link

    
14.06.2018 / 00:15