
Make Change
Hello all. Im taking AP Comp Sci and I'm little stuck on one of the programs we were assigned.
The program basically takes a user input of cents and converts that into all possible combinations of quarters, dimes, nickels and pennies. It then prints out the total number of possible configurations.
For example:
Code:
How Many Cents: 6
6 cents = 0 quarters + 0 dimes + 0 nickels + 6 pennies
6 cents = 0 quarters + 0 dimes + 1 nickel + 1 pennies
There are 2 possible ways to make 6 cents using coins.
Here is what I have so far:
Code:
import java.util.Scanner;
public class MakeChange
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println ("How many cents? ");
int cents = input.nextInt();
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
while (cents > 0)
if (cents >= 25)
{
quarters++;
cents = 25;
}
else if (cents >= 10)
{
dimes++;
cents = 10;
}
else if (cents >= 5)
{
nickels++;
cents = 5 * nickels;
}
else if (cents >= 1)
{
pennies++;
cents = 1;
}
System.out.println(cents + " cents = " + quarters + " quarters + " + dimes + " dimes + " + nickels + " nickels + " + pennies + " pennies");
}
}
However, I know this is wrong because it only prints out the combination with the least amount of coins. It is supposed to print ALL possible combinations. Can anyone help?
Also, Im supposed to be using nested loops for these.
Thanks Again.

First of all, do you have any algorithm to find those all combination?

No. That is my main problem right now....Finding all the possible combinations.


Most probably you have to think about permutations. It's the easiest way to figure out the way of selections. Then try to convert them into steps.