Question

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!!

Was it helpful?

Solution

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";

OTHER TIPS

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);
$times = floor($amount / 5000);
$remainder = $amount % 5000;
<?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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top