Need Help with Program

Status
Not open for further replies.

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:

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.
 
here this is a general algorithm that should get you close
since it is an assignment I will not give you the actual code.

//make sure this is integer division
intQuartCase = cents / 25

for each quarter case
//starting with most quarters first
quarters = intQuartCase - quarter loop counter

//calculate how many cents are left using this number of quarters
qNewCents = cents - ( 25 * quarters )

//now repeat for dimes
intDimeCase = qNewCents / 10

for each dime case
dimes = intDimeCase - dime loop counter
dNewCents = qNewCents - (10 * dimes)

//now repeat for nickels
//now pennies equals dNewCents - ( 5 * nickels ) because after calculating the nickels
// all that is left is the pennies
//before exiting the nickels for loop add one to a counter
//print quarters dimes nickels pennies​
//end nickels loop​
//end dimes loop​
//end quarters loop
//print number of possibilities using counter mentioned in nickels loop
 
What a horrible project. With 6 cents well that's OK but what about 80 cents. This would have to display
80 cents,
(80 cents - theSizeOfYourNextDenomination) 1
(80 cents - 2* theSizeOfYourNextDenomination) 2
...
yawn

I hate pointless programs

EDIT: And php, while I remember, why the aversion to mod?
 
for the record make sure in each case include the zero case.

and kmote, I don't think mod would work in this case. It should actually be the floor( cents / 25 )...I am not sure of how java handles integer division. But back to mod...if cents is 50 then then 50 mod 25 = 0. Doesn't really tell you much except that 50 can be evenly divided by 25. If that is not what you are talking about then I am not sure where you would use it.
 
Please note this is not the way I would do it, just an example

cents = getUserInput()

quarters = cents/25
theRest = cents%25

dimes = theRest/10
theRest = theRest%10

Also please excuse me if I'm being a fool and missing something obvious. I've not got nearly enough sleep as of late.
 
quarters = cents/25
theRest = cents%25

doesn't work for every iteration.

if the user inputs 55 cents it works for 2 quarters but how do you account for only one quarter?
 
Thanks all for the help. I got the program working a few days ago. :D

Code:
/**
 * This program takes a user input of money in cents and prints all the possible
 * representations of that amount as a combination of quarters, deimes, nickels, 
 * and pennies. It then displays the total number of possible configurations. 
 *
 */

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();            // User input of cents
    
    int counter = 0;   // declares the variable counter which keeps track of total configurations
   
    for(int quarters = 0; quarters <= cents / 25; quarters++)   // finds number of quarters
    {
      int centsAfterQuarters = cents - (quarters * 25);
            
      for(int dimes = 0; dimes <= centsAfterQuarters / 10; dimes++)     // finds number of dimes
      {
        int centsAfterDimes = centsAfterQuarters - (dimes * 10);
         
        for(int nickels = 0; nickels <= centsAfterDimes / 5; nickels ++)     // finds number of nickels
        {
          int centsAfterNickels = centsAfterDimes - (nickels * 5);            // remaining cents are the pennies         
          counter++;           //increments number of configurations after each iteration by 1
          
          System.out.println(cents + " cents = " + quarters + " quarters + " + dimes + " dimes + " + nickels + " nickels + " + centsAfterNickels + " pennies");                 
        }
      } 
    }
    System.out.println("There are " + counter + " possible ways to make " + cents + " cents using coins.");
  } 
}
 
Status
Not open for further replies.
Back
Top Bottom