Write a program that calculates the difference between the sum of the even and the sum of the odd numbers in an array.
Examples:
Here is my solution:
<?php $input = readline(); $arr = explode(' ', $input); $even = 0; $odd = 0; foreach ($arr as $item) { if ($item % 2 == 0) { $even += $item; } else if ($item % 2 != 0) { $odd += $item; } } echo $even - $odd;
Hey, @eiorgert
Your input in one line can be:
$arr = array_map('intval', explode(' ', readline()));
622 questions
979 answers
129 comments
53 users