1 Replies - 3232 Views - Last Post: 14 October 2010 - 09:52 PM

#1 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Convert a Number into Roman Numerals

Posted 11 November 2009 - 02:44 PM

Description: To run the script, save it into "roman.rb", and run "ruby roman.rb"This script takes a number below 3999 and converts it into Roman numerals. Values above 3999 would required "barred" numerals.
# Hash holding the roman values
# Values above 3999 are not accepted, as numerals with bars are required.
roman_map = Hash[ 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"]

print "Enter number> "
arabic = gets.chomp.to_i
if arabic < 4000
  # Sort the keys, highest value (1000) first, then descending,
  # and parse values
  roman_map.keys.sort{ |a,b| b <=> a }.each do
    |n|
    # get roman numeral until the arabic number is too small,
    # in which case we'll try the smaller roman values
    while arabic >= n
      arabic = arabic-n
      print roman_map[n]
    end
  end
else 
  puts "Please enter number lower than 3999."
end



Is This A Good Question/Topic? 0
  • +

Replies To: Convert a Number into Roman Numerals

#2 febds_020390   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-October 10

Re: Convert a Number into Roman Numerals

Posted 14 October 2010 - 09:52 PM

would you help me to have a program like yours, but in matlab code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1