Help with Change program, teacher cant teach

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 1
Reputation: rkp803 is an unknown quantity at this point 
Solved Threads: 0
rkp803 rkp803 is offline Offline
Newbie Poster

Help with Change program, teacher cant teach

 
0
  #1
Aug 23rd, 2008
i need help writing a program where i input a Cent vaule and it should give me a value in how many ways can u create change for it in half dollars,quarters, nickels, dimes, pennys. for example if i typed 100 which is one dollar it should say 292. this is what i have now im a noob soo someone help me write it.

class Change
{
public static void main(String args[]) {
keyboard key=new keyboard();

int count=0;
int amount = key.nextInteger();

for(int a=0; a<=amount/100; a++)
for(int b=0; b<=amount/50; b++)
for(int c=0; c<=amount/25; c++)
for(int d=0; d<=amount/10; d++)
for(int e=0; e<=amount/5; e++)
for(int h=0; h<=amount/1; h++)

System.out.println(count); }}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Help with Change program, teacher cant teach

 
1
  #2
Aug 23rd, 2008
well, 1 please use code tags.
2 why don't you use the modulo '%' operator.
  1. int cents=197;
  2. int dollars = cents/100;//shows dollars
  3. int temp = cents%100;//gets cents left over
  4. int halfs = temp/50;//gets amout of half dollars
  5. temp = temp%50;//gets cents left over
  6. int quarts = temp/25;//gets quarters
  7. temp = temp%25;//gets cents left over
  8. int dimes=temp/10;//gets dimes
  9. temp = temp%10;//gets cents left over
  10. int nicls = temp/5;//gets nickels
  11. temp = temp%5;//gets cents left over
  12. int pens = temp;//gets pennies
this only gets one way to do it, but i think there is a way to change it to make it work
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: Help with Change program, teacher cant teach

 
0
  #3
Aug 23rd, 2008
I would use a recursive approach for this problem. here is the logic:
int changeCombinationNumber(int change)

if it is less then 5 return 1;(ve are returning 1 because if it less then 5 there is only one possibility you can only use cents this is out escape from recursife loop)

if chance>50 return changeCombinationNumber(50)*changeCombinationNumber(change-50)
else if change>25 return changeCombinationNumber(25)*changeCombinationNumber(change-25)
etc...

ok for example if it is 25 change will also be onlay a quatter. add that control to your algorithm..it hass a lot of missing part but thats how I would start, I hope thats help
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with Change program, teacher cant teach

 
1
  #4
Aug 23rd, 2008
your teacher can teach just fine. But you're too stupid or lazy to learn.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC