/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def binary_to_decimal(x) y = 1 z = 0 x.gsub(/ /,'').reverse.scan(/(0|1)/).each do |x| z = z + (y * x[0].to_i) y *= 2 end z end number = gets.chomp puts binary_to_decimal(number)