Ask Experts Questions for FREE Help !
Ask
    CNJNLUV's Avatar
    CNJNLUV Posts: 7, Reputation: 1
    New Member
     
    #1

    Apr 1, 2008, 02:30 PM
    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.
    retsoksirhc's Avatar
    retsoksirhc Posts: 912, Reputation: 71
    Senior Member
     
    #2

    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Edge detection algorithm [ 3 Answers ]

Hi all. I need your help in implementing a hough trasform to find pupil and iris. I need to implement iris recognition algorithm for my final year project. It is part of project. So anyhow I need to find methology to edge detection algorithm. Help to do this. Time is going fast. (source code...

Program and algorithm [ 1 Answers ]

HI ,, CUD ANYONE GIVE ME A WORKING ALGORITHM AND PROGRAM IN C ABOUT LCM AND MODE , MEDIAN ,,AND HCF,, IT SHULD WORK IN LINUX OS TOO.. THANK..U..:o :)

Structure of the algorithm [ 2 Answers ]

Write a set of instructions using while loop and if... else Selection structures to sort the pile of cards according to the Symbols they display and write an appropriate message for each Card, for example 'shaded circle' or 'unshaded square'. Each Message should be on a new line. The...

Using random_shuffle from <algorithm> [ 1 Answers ]

I can't seem to get 'random_shuffle' from <algorithm> to work correctly. I have defined a class 'aCard' which has data members 'aRank' and 'aSuit'. Then define a vector of aCards called 'deck'. I initialize the deck with 'initDeck()' and try to shuffle the deck with 'shuffleDeck()'. Just for...


View more questions Search