Advertisement
epheterson

$15, $1/chocolate, 3 wrappers/chocolate, find max

Nov 5th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2.  
  3. // You can run this code at http://writecodeonline.com/php/
  4.  
  5. $dollars = 1502;
  6.  
  7. $cost = 3; // Dollars per chocolate
  8. $ratio = 3; // Wrappers to receive another chocolate
  9.  
  10. $chocolates = $wrappers = floor($dollars / $cost);
  11.  
  12. while ($wrappers >= $ratio)
  13. {
  14.         $wrappers = $wrappers - $ratio + 1;
  15.         $chocolates++;
  16. }
  17.  
  18. echo "$" . $dollars . " gets you " $chocolates . " chocolates with " . $wrappers . " wrapper(s) and $" . $dollars % $cost . " left over.";
  19.  
  20. // Example Output:
  21. // $1502 gets you 749 chocolates with 2 wrapper(s) and $2 left over.
  22.  
  23. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement