/ Published in: Ruby
Expand |
Embed | Plain Text
def decimal_to_binar(x) z = [] while x >= 1 z << x % 2 x /= 2 end z.reverse.join(" ") end number = gets.chomp puts "decimal : #{number}nbinary : #{decimal_to_binar(number.to_i)}"
Comments
Subscribe to comments
You need to login to post a comment.

why not just do: decimalnum.tos(2) 5.to_s(2) # => "101"