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

  • برمجة بي اتش بي

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

الأجوبة

<?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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...