View Single Post
Old 12-17-2008, 10:43 PM   #1 (permalink)
Black.Ice.
Black.Ice.'s Avatar
 
Junior Techie

Join Date: Aug 2007

Location: Boston, MA

Posts: 54

Black.Ice. is on a distinguished road

Send a message via AIM to Black.Ice. Send a message via MSN to Black.Ice.
Default Need Help with Program

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.
__________________
$500 rig ftw:

Case: CM 690 Black ATX Mid Tower
Mobo ASUS M2N32-SLI Deluxe AM2 nForce 590 SLI
CPU: AMD Athlon 64 X2 6400+ Black Edition @ 3.2GHz 23 C idle 37 load :O
GFX: 8600 GT XXX 256 mb
RAM: Corsair XMS2 2GB DDR2 800 mhz PC2 6400
HDD: Seagate Barracuda ST3320620AS 320GB 7200 RPM w/ Slave Western Digital Caviar WD800BB 80GB 7200 RPM
HSF: Zalman CNPS9700 LED 110mm 2 Ball
PSU: CM eXtreme Power Dual 12V rails @ 36A 650W

Tee Hee!
Black.Ice. is offline