# Recursive Change Machine

• 04-01-2011, 02:37 AM
EZlats
Recursive Change Machine
I need to create a change machine using recursion but the exact directions state "The method will display all combinations of quarters, dimes, nickels, and pennies that equal the desired amount."
What that sounds like it's asking is for me to print out all possible coin combinations that could be used to give change. ie \$1 = 100 pennies, 95 pennies and 1 nickel, 90 pennies and 2 nickels, 90 pennies and one dime,....., 4 quarters.
I have asked whether or not I really need to find that or not, but I have a lot to do so in case it is, I need to figure out how I could do it.
Now I have written code that should return the least amount of coins needed and this is the meat and potatoes of it. This is not the main method, it will print out how many of each coin will be needed in the main.

Code:

```//ChangeMostQuarters will be called first, each change method gives as many of that coin as possible public class ChangeMachine {     private static int pennies = 0;     private static int nickels = 0;     private static int dimes = 0;     private static int quarters = 0;     public static void ChangeAllPennies(int exchange){         if(exchange >= 1){             pennies++;             exchange--;         }         ChangeAllPennies(exchange);     }     public static void ChangeMostNickels(int exchange){         if(exchange >=5){             nickels++;             exchange-=5;             ChangeMostNickels(exchange);         }         else             ChangeAllPennies(exchange);     }     public static void ChangeMostDimes(int exchange){         if(exchange >= 10){             dimes++;             exchange-=10;             ChangeMostDimes(exchange);         }         else             ChangeMostNickels(exchange);         return;     }     public static void ChangeMostQuarters(int exchange){             if(exchange >=25){                 quarters++;                 exchange -= 25;                 ChangeMostQuarters(exchange);             }             else                 ChangeMostDimes(exchange);             return;     } }```
If anyone has any idea on how I could change this or how I could call these in my main to actually get and then display all possible combinations of coins, I'd really appreciate your help. Thanks
• 04-01-2011, 03:42 AM
Solarsonic
What's with the "return;" in some of the voids? Is there any reason for that?

Besides that the code looks fine, what errors have you gotten?
• 04-01-2011, 03:46 AM
Junky
Having a return statement in a void method is perfectly fine. However it is pointless as the last line of code in the method.

You might want to seek clarification from your teacher but I imagine this is supposed to be done with a single recursive method.