Results 1 to 5 of 5
Thread: Make Change
- 12-18-2008, 04:03 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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:
Here is what I have so far:Java 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.
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?Java 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"); } }
Also, Im supposed to be using nested loops for these.
Thanks Again.Last edited by Black.Ice.; 12-18-2008 at 04:32 AM.
- 12-18-2008, 05:14 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, do you have any algorithm to find those all combination?
- 12-18-2008, 05:48 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
No. That is my main problem right now....Finding all the possible combinations.
- 12-18-2008, 06:55 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
I got it figured out. :P
- 12-18-2008, 07:12 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Similar Threads
-
How to change JDK?
By mew in forum EclipseReplies: 5Last Post: 05-21-2010, 05:21 AM -
Change in O/p
By shiv_122 in forum Advanced JavaReplies: 4Last Post: 12-22-2008, 02:37 AM -
ip change
By iiggoorr in forum NetworkingReplies: 9Last Post: 11-05-2008, 07:11 PM -
Change my for loop
By javaplus in forum New To JavaReplies: 4Last Post: 12-12-2007, 11:00 AM -
Is it possible to change the '\n' into ' ' ...
By johnny7white in forum New To JavaReplies: 1Last Post: 11-15-2007, 02:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks