Decimal to Binary Terminal Script


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



Copy this code and paste it in your HTML
  1. def decimal_to_binar(x)
  2. z = []
  3. while x >= 1
  4. z << x % 2
  5. x /= 2
  6. end
  7. z.reverse.join(" ")
  8. end
  9.  
  10. number = gets.chomp
  11. puts "decimal : #{number}nbinary : #{decimal_to_binar(number.to_i)}"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.