Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- echo add ('109', '99')."\n";
- function add ($x, $y) {
- $a = strlen ($x);
- $b = strlen ($y);
- $l = abs ($a - $b);
- $c = max ($a, $b) - 1;
- $u = 0;
- $r = '';
- if ($a > $b) $y = str_repeat ('0', $l) . $y;
- if ($b > $a) $x = str_repeat ('0', $l) . $x;
- while ($c != 0) {
- $d = $x[$c] + $y[$c] + $u;
- echo "$d\n" ;
- if ($d >= 10) {
- $r .= ($d % 10) + $u;
- $u = ($d / 10) % 10;
- } else {
- $r .= $d + $u;
- $u = 0;
- }
- $c--;
- }
- $r .= strrev ($x[0] + $y[0] + $u);
- return strrev ($r);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement