Results 1 to 5 of 5

Thread: Make Change

  1. #1
    Black.Ice. is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default 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:

    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.
    Here is what I have so far:

    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");
      }
    }
    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.
    Last edited by Black.Ice.; 12-18-2008 at 05:32 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Black.Ice. is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

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

  4. #4
    Black.Ice. is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    I got it figured out. :P

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 06:21 AM
  2. Change in O/p
    By shiv_122 in forum Advanced Java
    Replies: 4
    Last Post: 12-22-2008, 03:37 AM
  3. ip change
    By iiggoorr in forum Networking
    Replies: 9
    Last Post: 11-05-2008, 08:11 PM
  4. Change my for loop
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 12-12-2007, 12:00 PM
  5. Is it possible to change the '\n' into ' ' ...
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 03:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •