943,535 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2805
  • Java RSS
Aug 23rd, 2008
0

Help with Change program, teacher cant teach

Expand Post »
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); }}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkp803 is offline Offline
1 posts
since Aug 2008
Aug 23rd, 2008
1

Re: Help with Change program, teacher cant teach

well, 1 please use code tags.
2 why don't you use the modulo '%' operator.
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 23rd, 2008
0

Re: Help with Change program, teacher cant teach

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
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006
Aug 23rd, 2008
1

Re: Help with Change program, teacher cant teach

your teacher can teach just fine. But you're too stupid or lazy to learn.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Programming Help!!!!!!!!!!!!!!!!!
Next Thread in Java Forum Timeline: Order Processing System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC