Binary to decimal terminal script


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def binary_to_decimal(x)
  2. y = 1
  3. z = 0
  4. x.gsub(/ /,'').reverse.scan(/(0|1)/).each do |x|
  5. z = z + (y * x[0].to_i)
  6. y *= 2
  7. end
  8. z
  9. end
  10.  
  11. number = gets.chomp
  12. puts binary_to_decimal(number)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.