Calculating change based on input I decided to make a program that calculates change, based on an input. My code is extremely long, and I would like to know how I can fix this.

def make_change(amount)
  values = []
  coins = []
  hash = {}
  if amount &gt;= 50
    coins.push(:H)
    values.push(amount / 50)
    amount = amount % 50
    if amount % 50 &gt;= 25
      coins.push(:Q)
      values.push(amount / 25)
      amount = amount % 25
      if amount % 25 &gt;= 10
        coins.push(:D)
        values.push(amount / 10)
        amount = amount % 10
        if amount % 10 &gt;= 5
          coins.push(:N)
          values.push(amount / 5)
          amount = amount % 5
          if amount % 5 &gt;= 1
            coins.push(:P)
            values.push(amount / 1)
          end
        end
      end
    end
  elsif amount &gt;= 25
    coins.push(:Q)
    values.push(amount / 25)
    amount = amount % 25
    if amount % 25 &gt;= 10
      coins.push(:D)
      values.push(amount/10)
      amount = amount % 10
      if amount % 10 &gt;= 5
        coins.push(:N)
        values.push(amount/5)
        amount = amount % 5
        if amount % 5 &gt;= 1
          coins.push(:P)
          values.push(amount/1)
        end
      end
    end
  elsif amount &gt;= 10  
    coins.push(:D)
    values.push(amount / 10)
    amount = amount % 10
    if amount % 10 &gt;= 5
      coins.push(:N)
      values.push(amount / 5)
      amount = amount % 5
      if amount % 5 &gt;= 1
        coins.push(:P)
        values.push(amount / 1)
      end
    end
  elsif amount &gt;= 5
    coins.push(:N)
    values.push(amount / 5)
    amount = amount % 5
    if amount % 5 &gt;= 1
      coins.push(:P)
      values.push(amount / 1)
    end
  elsif amount &gt;= 1
    coins.push(:P)
    values.push(amount / 1)
  else
    coins = []
    values = []
  end
  Hash[coins.zip(values)]
end


Examples

make_change(42) should result in
   => {:Q => 1, :D => 1, :N => 1, :P => 2

make_change(91) should result in
   => {:H => 1, :Q => 1, :D => 1, :N => 1, :P => 1
Here's one way, that uses the method Fixnum#divmod to advantage:

COINS = [["H", 50], ["Q", 25], ["D", 10], ["N", 5], ["P", 1]]

def change(amount)
  raise ArgumentError, 'Only non-negative integers permitted' unless
    amount.is_a?(Fixnum) &amp;&amp; amount &gt;= 0
  return [] if amount.zero?
  COINS.map do |label, value|
    nbr, amount = amount.divmod(value)
    [nbr, label]
  end
end

change(260) #=&gt; [[5, "H"], [0, "Q"], [1, "D"], [0, "N"], [0, "P"]]
change(95)  #=&gt; [[1, "H"], [1, "Q"], [2, "D"], [0, "N"], [0, "P"]]
change(100) #=&gt; [[2, "H"], [0, "Q"], [0, "D"], [0, "N"], [0, "P"]]
change(80)  #=&gt; [[1, "H"], [1, "Q"], [0, "D"], [1, "N"], [0, "P"]]
change(45)  #=&gt; [[0, "H"], [1, "Q"], [2, "D"], [0, "N"], [0, "P"]]
change(16)  #=&gt; [[0, "H"], [0, "Q"], [1, "D"], [1, "N"], [1, "P"]]
change(1)   #=&gt; [[0, "H"], [0, "Q"], [0, "D"], [0, "N"], [1, "P"]]
change(0)   #=&gt; []
change(-3)  #=&gt; ArgumentError: Only non-negative integers permitted...
change(1.2) #=&gt; ArgumentError: Only non-negative integers permitted...
change('c') #=&gt; ArgumentError: Only non-negative integers permitted...