Skip to content

Instantly share code, notes, and snippets.

@blurredbits
Created March 13, 2014 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blurredbits/9520646 to your computer and use it in GitHub Desktop.
Save blurredbits/9520646 to your computer and use it in GitHub Desktop.
Roman Numeral Generator - J. Weirich edition.
puts "Please enter a number you would like to convert to a roman numeral"
num = gets.chomp.to_i
def to_roman(num)
results = ""
digits = [
[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"]
]
digits.each do |number, symbol|
while num >= number
results << symbol
num -= number
end
end
results
end
puts "Your roman numeral is: " + to_roman(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment