-1

I need to write a PHP script that calculates the No. of time it divides a number and reminder too. Lets say $amount=9200; if I divide this with 5000 then output should be 5000: 1 times and Reminder: 4200. I think i need to use $n=$amount%5000; But I got only reeminder not the no. of times it divides.

Thanks!!

Its Me
  • 3
  • 1
  • 2
  • For "ATM machine", see http://www.urbandictionary.com/define.php?term=redundant%20acronym – SDC Feb 06 '13 at 13:52

4 Answers4

2

This is well-known as the euclidean division : http://en.wikipedia.org/wiki/Euclidean_division

$amount = 9200;
$divide = 5000;
$times = floor($amount/$divide);
$reminder = $amount%$divide;

echo "$amount = $times times $divide plus $reminder";
mimipc
  • 1,354
  • 2
  • 14
  • 28
  • He already has the remainder. He wants the times. `floor()` is more than capable here. – BenM Feb 06 '13 at 13:51
  • @BenM - actually, `floor()` returns a `float` value and dividing+flooring is more expensive in terms of processing than the multiplication option (ie `$divide * $times`). You need two operations either way to get the full answer; you may as well use the one that performs better. – SDC Feb 06 '13 at 13:56
0

The % operator will give you the remainder and then you will need to do another operation to get the number of times that you divided.

$times = floor($amount/5000);
Schleis
  • 41,516
  • 7
  • 68
  • 87
0
$times = floor($amount / 5000);
$remainder = $amount % 5000;
BenM
  • 52,573
  • 26
  • 113
  • 168
0
<?php

class  ATM
{
    public function deliver( $note )
    {
        // code to grab that not from the cash boxes...
    }
}

$notes = new SplFixedArray(5);
$notes[0] = 100;
$notes[1] = 50;
$notes[2] = 20;
$notes[3] = 10;
$notes[4] = 5;

$notesKey = 0;

$withdraw = 920;
$allocated = 0;

$deliver = new SplQueue();

// work out biggest notes for remaining value and queue
while($allocated < $withdraw)
{
    $remains  = ($withdraw-$allocated) % $notes[$notesKey];
    $numNotes = (($withdraw-$allocated)-$remains)/$notes[$notesKey];
    for( $i = 0; $i < $numNotes; $i++ )
    {
        $allocated += $notes[$notesKey];
        $deliver->enqueue($notesKey);
    }
    ++$notesKey;
}

$atm = new ATM();
while(!$deliver->isempty())
{
     $atm->deliver($notes[$deliver->dequeue()]);
}
?>

Something like that should do the trick...

Ian Wood
  • 6,515
  • 5
  • 34
  • 73