SHARE
TWEET

05. Multiply Big Number

MartinGeorgiev Mar 28th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $bigNumber = readline();
  3. $digit = intval(readline());
  4. $resultNumber = '';
  5. $result = 1;
  6. $onMind = 0;
  7.  
  8. $reversedBigNum = strrev($bigNumber);
  9.  
  10. if($bigNumber === '0' || $digit === 0){
  11.     echo '0';
  12.     return;
  13. }
  14.  
  15. for($i = 0; $i < strlen($reversedBigNum); $i++){
  16.     $firstDigit = intval($reversedBigNum[$i]);
  17.  
  18.     $result = $firstDigit * $digit + $onMind;
  19.     $resultNumber .= $result % 10;
  20.     $onMind = intval($result / 10);
  21.  
  22.     if($i === (strlen($reversedBigNum)-1) && $onMind !== 0){
  23.         $resultNumber .= $onMind;
  24.     }
  25. }
  26.  
  27. echo strrev($resultNumber);
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