Amount of coin change

Topic Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cent. For example, if the amount is 86 cents, the ouput is like
86 cents can be given as
3 quarters, 1 dime, 1 penny

should use this function
void compute_coin (int coin_value, int& number, int& amount_left);

Can someone help me with this program, i dont know how to start it.
Who is Participating?
just a small correction:
>> hint: 86/3 => 3 quarters
should be 86/25 => 3 quarters
and if you want to be more picky, there are half-dollar coins too... although they are not used very often mainly because of their size and weight.

r-k gave you the high level algo. here's my interpretation of the parameters.
coin_value: pretty straight fwd - 1 for penny, 5 for nickel, 10 for dime...
number: the max number of coins that can fit in the given amount
amount_left: the given amount. when we subtract coin_value*number, this will be interpreted as amount_left
#define QUARTER 25
amount_left = 86;
compute_coin(QUARTER, number, amount_left);
//at this point, number = 3 and amount_left = 11.
#undef QUARTER
#undef HALF_DOLLAR  //etc...
//end of code
This sounds like homework, so I can't do it for you. But I can give you a few hints:

We can assume the question is to give out change with the least number of coins. For example, 86 cents could also be given out as 86 pennies, or as 8 dimes and 6 pennies, etc.

Given that, you could start by seing how many of the largest coin (i.e. 25 cents) would fit within 86 cents (hint: 86/3 => 3 quarters). That leaves 11 cents, then you repeat for next largest coin, and so on.

Hope i did not give too much help.
An enumeration may be a good helper:

enum Coin { DOLLAR = 100, QUARTER = 25, DIME = 10, NICKEL = 5, PENNY = 1, NUM_COINS = 5 };

Your main would be like that:

int main()
      double   input = 1.;
      int         cents;   // total
      int         dollars;
      int         quarters;
      int         dimes;
      int         pennies;
      while (true)
           // get input (cout/cin)
           // break if input less or equal 0.
           // convert dollar input to cents
           // check how many dollar coins could be given out
           // subtract cent equivalent to given dollar coins from cents
           // check how many quarter coins could be given out
           // subtract cent equivalent to given quarter coins from cents
           // do same for dime, nickel
           // remainder in cents is pennies          
           // output the result
           // you could have two strings for each coin (singular and plural)
           // you could make string arrays where NUM_COINS is the size of the arrays
           // if a coin value is 0 you should suppress output


Regards, Alex
How about the 50 cent coins.  It could be 3 quarters or a half dollar and a quarter.  Anyway, the principle is the same whatever the coinage.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.