Computer Science Canada

Expressing a dollar amount as different coins.

Author:  GDoughtUpInIt [ 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.

Author:  Tony [ Wed Jul 14, 2004 2:25 pm ]
Post 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

Author:  rizzix [ Wed Jul 14, 2004 2:43 pm ]
Post subject: 

use modulus!! heh

Author:  wtd [ 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

Author:  wtd [ Wed Jul 14, 2004 3:09 pm ]
Post 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


: