Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Expressing a dollar amount as different coins.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GDoughtUpInIt




PostPosted: Wed Jul 14, 2004 12:22 pm   Post subject: Expressing a dollar amount as different coins.

Hello everyone.

I was wondering, how can I take a number such as $104.56 (or any other number) and express it as 5 twenty dollar bills, 4 loonies, 2 quarters, and 6 pennies? Any help will be GREATLY appreciated. Thank you.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jul 14, 2004 2:25 pm   Post subject: (No subject)

wouldn't it be 2 toonies? Laughing

anyways, what you do is :
code:

see if you can subtract largest bill/coin without going into negatives.
if so -> mark down the bill/coin and try to take another one out.
if not -> reduce bill/coin size to one lower (keep valid sizes in an array)
continue untill you can't take out any more pennies
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
rizzix




PostPosted: Wed Jul 14, 2004 2:43 pm   Post subject: (No subject)

use modulus!! heh
wtd




PostPosted: Wed Jul 14, 2004 3:03 pm   Post subject: Re: Expressing a dollar amount as different coins.

GDoughtUpInIt wrote:
Hello everyone.

I was wondering, how can I take a number such as $104.56 (or any other number) and express it as 5 twenty dollar bills, 4 loonies, 2 quarters, and 6 pennies? Any help will be GREATLY appreciated. Thank you.


Well, you should take toonies out too. Wink
wtd




PostPosted: Wed Jul 14, 2004 3:09 pm   Post subject: (No subject)

Note: Always use integers to represent dollars and cents, to avoid floating point inaccuracy.

Anyway, at least the first few steps of this are going to look like:

code:
original_amount = 10456

number_of_twenties = original_amount / 2000
amount_left_over = original_amount % 2000

number_of_toonies = amount_left_over / 200
amount_left_over = amount_left_over % 200
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: