Black.Ice.
Baseband Member
- Messages
- 55
- Location
- Boston, MA
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:
Here is what I have so far:
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.
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.