Created
November 21, 2013 16:47
-
-
Save faizaanshamsi/7585269 to your computer and use it in GitHub Desktop.
#Assuming amount tendered is greater or equal to amount due
#Does not format output to 2 decimals
#Assumes inputs are in 2 decimal or less format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
change = { | |
:dollars => 100, | |
:quarters => 25, | |
:dimes => 10, | |
:nickels => 5, | |
:pennies => 1 | |
} | |
puts "What is the amount due?" | |
amount_due = gets.chomp.to_f | |
change_types = {} | |
puts "What is the amount tendered?" | |
tender = gets.chomp.to_f | |
change_due_x100 = ((amount_due - tender) * 100).to_i | |
change.each do |change, value| | |
change_types[change] = (change_due_x100 / value) | |
change_due_x100 -= change_types[change] * value | |
end | |
puts "The change due is: $#{(amount_due-tender).round(2)}" | |
puts "You should issue:" | |
change_types.each do |type, quantity| | |
puts "#{quantity} #{type}" unless quantity == 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment