codingtests/solutions/other/reversemultiple9.php
2024-07-23 09:13:29 +00:00

27 lines
548 B
PHP

<?php
//Find a three-digit number such that when the digits are reversed and subtracted from the original number, the result is a multiple of 9.
function numreverse($number) {
$numberStr = (string)$number;
$numberStr = strrev($numberStr);
return (int)$numberStr;
}
$i = 100;
$good = 0;
$bad = 0;
while($i <=999) {
$num = $i - numreverse($i);
if($num != 0 && $num % 9 == 0) {
echo("\nFound $i");
$good++;
} else {
echo("\nNo $i");
$bad++;
}
$i++;
}
echo "\nGood: $good Bad: $bad";