# randomly generates an amount of change and then calculates the fewest

Page 1 of 1

## 3 Replies - 1926 Views - Last Post: 26 March 2009 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=95501&amp;s=1fe79ccabb46bf1ecb98a62fd49de411&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gilcohen007

Reputation: 0
• Posts: 4
• Joined: 18-March 09

# randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 11:05 AM

an application that randomly generates an amount of change and then calculates the fewest coins required for that amount. The possible coins are quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent). The amount should be a random number between 0 and 99, inclusive. the program should generate a random number every time it runs.
For example, if the program randomly generates the number 57, then the program should output something like this:
The change of 57 cents yields 2 quarters, 1 nickels and 2 pennies.

I cant figure it out exactly here is what I have

```   import javax.swing.*;

import java.util.Scanner;

import java.util.Random;

// Generate random #
public  class pppp {

public static final void main(String[] args){

//note a single Random object
Random randomGenerator = new Random();

for (int idx = 1; idx <= 1; ++idx){
int randomInt = randomGenerator.nextInt(100);
log("random # is : " + randomInt);
}

}

private static void log(String aMessage){
System.out.println(aMessage);

int num;
int qwater =  num /25;
int  nikle = num /10;
int dime = num /5;
int peny = num /5;

System.out.println ("qwaters: " + qwater );
System.out.println ("nikle: " +   nikle );
System.out.println ("dime: " +  dime );
System.out.println ("peny: " +  peny );

}
}

```

Is This A Good Question/Topic? 0

## Replies To: randomly generates an amount of change and then calculates the fewest

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 06:56 PM

You forget to remove from the amount to give back what was already gave back with higher coins

int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);

Hope this helps

### #3 gilcohen007

Reputation: 0
• Posts: 4
• Joined: 18-March 09

## Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 08:10 PM

thanks this is very helpful but i still have one error which i am not really sure?

here is my error:
//cannot find symbol
//symbol : variable quarter
//location: class pppp
//amount = amount - (quarter * 25);
^
//1 error

and here is my code.

```	import javax.swing.*;

import java.util.Scanner;

import java.util.Random;

// Generate random #
public  class pppp {

public static final void main(String[] args){

//note a single Random object
Random randomGenerator = new Random();

for (int idx = 1; idx <= 1; ++idx){
int randomInt = randomGenerator.nextInt(100);
log("random # is : " + randomInt);
}

}

private static void log(String aMessage){
System.out.println(aMessage);

int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);
//int penny = amount / 1;
//int cent = amount - (penny * 1);

System.out.print("quaters: " + quater );
System.out.print("dime: " +   dime );
System.out.print("nickel: " +  nickel );
System.out.print("penny: " +  cent );

}
}

```

thanks a lot

pbl, on 26 Mar, 2009 - 05:56 PM, said:

You forget to remove from the amount to give back what was already gave back with higher coins

int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);

Hope this helps

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 08:18 PM

If you can't find a single stupid typo don't predict you a great future in programming

int amount;
int quater = amount / 25; <------ r missing in quarter
amount = amount - (quarter * 25); <------ that is used here
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);