I came across a programming challenge that i thought might be cool to solve. Its about writing a program to find out how much of coins must be given for a particular money. For instance in Ghana, we have the 100p,50,20,10,5 and 1p.(p for pesewes). Thus if i have 75p then i can be given a change of
1 of 50p,1 of 20p and 5p
which adds up to 75p.
Now this is how the program goes.
a = [50,20,10,5,1]
money = ARGV[0].to_i
remainder = 0
hash = Hash.new(0)
a.each do |i|
remainder = money/i
next if remainder == 0
hash[i] =remainder
money=money%i
end
#extra information.
sum=0
ash.sort.reverse.each do |i,j|
sum=sum+(i*j)
puts “#{j} of #{i}”
end
puts “=======”
puts ” #{sum} Pesewas”
Explanation:
First, we store store out 75p in money. Having the denominations in an array, we extract each item. If the

No comments yet
Comments feed for this article