Q:

Write a PHP program to compute the sum of two given non-negative integers x and y as long as the sum has the same number of digits as x

0

Write a PHP program to compute the sum of two given non-negative integers x and y as long as the sum has the same number of digits as x. If the sum has more digits than x then return x without y

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($x, $y)
{
   return strlen((string)($x + $y)) > strlen((string)$x) ? $x : $x + $y;
}

echo (test(4, 5))."\n";
echo (test(7, 4))."\n";
echo (test(10, 10))."\n";

Sample Output:

9
7
20

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now