Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Programming (https://www.askmehelpdesk.com/forumdisplay.php?f=437)
-   -   Algorithm Inquiry (https://www.askmehelpdesk.com/showthread.php?t=201242)

  • Apr 1, 2008, 02:30 PM
    CNJNLUV
    Algorithm Inquiry
    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.
  • Apr 8, 2008, 12:42 PM
    retsoksirhc
    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.

  • All times are GMT -7. The time now is 05:58 AM.