Results 1 to 2 of 2
Thread: Remainder  Coins
 11132012, 05:15 AM #1Member
 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()); } }
 11132012, 05:15 PM #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!
Similar Threads

I need help finding a remainder
By MrPosotive in forum New To JavaReplies: 2Last Post: 09242012, 07:01 AM 
Creating a program to determine least amount of coins with values that aren't factors
By Kore in forum New To JavaReplies: 6Last Post: 05292012, 01:47 PM 
how to discard remainder on division?
By RobertF in forum New To JavaReplies: 9Last Post: 03132009, 01:20 PM 
How to add coins in ascending order in arraylist
By tribujohn in forum New To JavaReplies: 2Last Post: 01232009, 05:31 AM 
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03212008, 05:07 PM
Bookmarks