1. Member
Join Date
Nov 2012
Posts
1
Rep Power
0

## Remainder - Coins

I am creating a program that will take a sum(ex/ \$1.71) and split it up into the least amount of coins.. Where I am stuck is how do I create the part to determine which coins to use. I am trying the remainder where it will see how many of each coin.. But where I am stuck is how do I write it out?

Like this?

sum % 1= pennies;
Then how would I get the new value of sum so I could continue on with nickels, dimes.. etc.

And my last questions is do I need to change my 1.71 to 171 so I can use divide by 100(dollars) 25(quarter) 10(dime) 5(nickel) 1(penny)

Here is my code if you wanted to see. For my assignment I need to create 2 classes.
Java Code:
```public class USCurrency {

int dollars, quarters, dimes, nickels, pennies;
double sum = dollars*1 + quarters*.25 + dimes*.1 + nickels*.05 + pennies*.01;

USCurrency(int nDollars, int nQuarters, int nDimes, int nNickels, int nPennies) {
sum % 5 = pennies;

}

public int getDollars(){
return dollars;
}

public int getQuarters(){
return quarters;
}
public int getDimes(){
return dimes;
}
public int getNickels(){
return nickels;

}
public int getPennies(){
return pennies;
}

public USCurrency sum (USCurrency a){
int sumDollars, sumQuarters, sumDimes, sumNickels, sumPennies;
sumDollars = this.dollars + a.getDollars();
sumQuarters = this.quarters + a.getQuarters();
sumDimes = this.dimes + a.getDimes();
sumNickels = this.nickels + a.getNickels();
sumPennies = this.pennies + a.getPennies();
return new USCurrency(sumDollars,sumQuarters,sumDimes,sumNickels,sumPennies);

}

}```

Java Code:
```public class TestUSCurrency {
public static void main(String[] args){
USCurrency jen = new USCurrency(1,0,0,0,141);
USCurrency bob = new USCurrency(0,4,3,2,1);
USCurrency total = jen.sum(bob);
System.out.[U][COLOR="#FF0000"]print[/COLOR][/U](jen.getDollars(), jen.getQuarters(), jen.getDimes(), jen.getNickels(), jen.getPennies());

}
}```
I also have an error right there for the 'print'. But I dont know why.

2. ## Re: Remainder - Coins

You need to convert into a standard unit, like pennies. From there, you can use mod with the various denomination amounts (100000, 50000, 20000, 10000, 5000, 1000, 25, 10, 5). It's pointless to % 1 because at that point, you already know how many pennies are left. Don't forget that you need to subtract each denomination from the total as you go!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•