4
28/10/2024 6:47 pm
Topic starter
Write a program that calculates the difference between the sum of the even and the sum of the odd numbers in an array.
Examples:
2 Answers
3
28/10/2024 6:48 pm
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;
2
28/10/2024 6:48 pm
Hey, your input in one line can be:
$arr = array_map('intval', explode(' ', readline()));