Shane Hudson, on 1 Feb, 2010 - 10:25 AM, said:
If anybody can help, please do!
Not sure if this is the best way, but it seems to work for me
def get_roman_numeral(int, rn)
values = {1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD', 100 => 'C', 90 => 'XC', 50 => 'L', 40 => 'XL', 10 => 'X', 9 => 'IX', 5 => 'V', 4 => 'IV', 1 => 'I'}
values.each_pair do |key, value|
if int >= key
rn.push(value)
return get_roman_numeral(int - key, rn)
end
end
return rn
end
puts 'Enter a number to be converted to a Roman numeral'
int = gets.chomp.to_i
original_int = int
roman_numeral = Array.new
roman_numeral = get_roman_numeral(int, roman_numeral)
numeral = ''
roman_numeral.each() do |r_numeral|
numeral = numeral + r_numeral
end
puts "#{original_int}, when converted to a Roman numeral, is #{numeral}!"
This post has been edited by Nakor: 01 February 2010 - 07:10 PM

New Topic/Question
Reply




MultiQuote










|