Change

Lua

Public Domain

Given an amount of money (in pennies), gives the coins to make it

Download (right click, save as, rename as appropriate)

Embed

Tags:

Fun, money
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/lua
-- change.lua -- works out the change for a given amount
-- Made by Joseph Salisbury -- 24/01/09

totalValue = tonumber( (arg[1]) ) -- The value to reach
-- The available denominations
coins = {200, 100, 50, 20, 10, 5, 2, 1}
coinNames = {
	[200] = "2 pound piece",
	[100] = "1 pound piece",
	[50] = "50 pence piece",
	[20] = "20 pence piece",
	[10] = "10 pence piece",
	[5] = "5 pence piece",
	[2] = "2 pence piece",
	[1] = "1 pence piece",
}

function usage()
	usage = "Usage: change.lua [amount]"
	return usage
end

if totalValue then
	while totalValue ~= 0 do
		for i=1, #coins do
			while totalValue >= coins[i] do
				print( coinNames[coins[i]] )
				totalValue = totalValue - coins[i]
			end
		end
	end
else
	print( usage() )
end