mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
18 lines
453 B
Ruby
18 lines
453 B
Ruby
|
# Iterative method to convert a given decimal number into binary.
|
||
|
|
||
|
def decimal_to_binary(n)
|
||
|
bin = []
|
||
|
until n.zero?
|
||
|
bin << n % 2
|
||
|
n = n / 2
|
||
|
end
|
||
|
return bin.reverse.join
|
||
|
end
|
||
|
|
||
|
puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s
|
||
|
# Binary value of 4 is 100
|
||
|
puts 'Binary value of 31 is ' + decimal_to_binary(31).to_s
|
||
|
# Binary value of 31 is 11111
|
||
|
puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s
|
||
|
# Binary value of 64 is 1000000
|