| | |
Help with Change program, teacher cant teach
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
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); }}
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); }}
well, 1 please use code tags.
2 why don't you use the modulo '%' operator.
this only gets one way to do it, but i think there is a way to change it to make it work
2 why don't you use the modulo '%' operator.
Java Syntax (Toggle Plain Text)
int cents=197; int dollars = cents/100;//shows dollars int temp = cents%100;//gets cents left over int halfs = temp/50;//gets amout of half dollars temp = temp%50;//gets cents left over int quarts = temp/25;//gets quarters temp = temp%25;//gets cents left over int dimes=temp/10;//gets dimes temp = temp%10;//gets cents left over int nicls = temp/5;//gets nickels temp = temp%5;//gets cents left over int pens = temp;//gets pennies
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
"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
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Java Programming Help!!!!!!!!!!!!!!!!!
- Next Thread: Order Processing System
Views: 1282 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






