CNJNLUV
Apr 1, 2008, 02:30 PM
I have an amount of $1.85 that I have to convert into quarters, dimes, nickels and pennies. The following is what I have come up with:
Q = Amount/25
Amount = Amount - 25 * Q
D = Amount/10
Amount = Amount - 10 * D
N = Amount/5
Amount = Amount - 5 * N
P = Amount
I want to put this in an While If Statement which I'm having problems with. How do I proceed? Thanks for any help.
retsoksirhc
Apr 8, 2008, 12:42 PM
A couple While's with a couple If's would do the trick, but I think it would be more convenient to have the program do some math... of course, depending on what language you're using.
Lets start the floating point value $money, and we'll calculate just how much of everything is needed by division and truncating. FYI: A good way to truncate, if you only have rounding functions, is to subtract .5 and then round to the nearest. Here's a PHP example
$quarters=floor($money/0.25);
$dimes=floor(($money-(0.25*$quarters))/0.10);
$nickels=floor(($money-(0.25*$quarters+0.10*$dimes))/0.05);
$pennies=floor(($money-(0.25*$quarters+0.10*$dimes+0.05*$nickels))/0.01);
To check it afterwards, you could do something like:
if($money != ($quarters*0.25 + $dimes*0.10... ){
//error handle
}
And I'm sure I forgot something somewhere in there, but that's the general idea of how I would do it, at first glance.