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); }}

8 Years
Discussion Span
Last Post by jwenting

well, 1 please use code tags.
2 why don't you use the modulo '%' operator.

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

this only gets one way to do it, but i think there is a way to change it to make it work

Votes + Comments
Well said, and a great sig :)

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)

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.