SHARE
TWEET

05. Multiply Big Number

MartinGeorgiev Mar 28th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $number = strrev(readline());
  3. $digit = intval(readline());
  4. $residue = 0;
  5. $multiply = 1;
  6.  
  7. if ($number == "0" || $digit == 0) {
  8.     echo "0";
  9.     return;
  10. }
  11.  
  12. for ($i = 0; $i < strlen($number); $i++) {
  13.     $firstDigit = intval($number[$i]);
  14.     $multiply = $firstDigit * $digit + $residue;
  15.     $product .= $multiply % 10;
  16.     $residue = intval($multiply / 10);
  17.  
  18.     if ($i === (strlen($number) - 1) && $residue !== 0) {
  19.         $product .= $residue;
  20.     }
  21. }
  22. echo strrev($product);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top