Created
January 18, 2016 02:29
-
-
Save Ebonkuab/6e864c5d3e09e10c52c8 to your computer and use it in GitHub Desktop.
ruby code to coNvert arabic (contemporary ) numbers to old style roman numerals. (LIGHTHOUSE WEB DEV BOOTCAMP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ROM ={ I: 1, | |
# V: 5, | |
# X: 10, | |
# L: 50, | |
# C: 100, | |
# D: 500, | |
# M: 1000 | |
# } | |
def to_roman(num) | |
@arrayOfRomanNumerals = [] # array to take in the generated roman numerals | |
value = num | |
div1 = value.divmod(1000) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"M") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby500 (div1[1]) | |
end | |
elsif div1[0] == 0 && div1[1] > 0 | |
divideby500 (div1[1]) | |
end | |
puts @arrayOfRomanNumerals.join("") | |
end | |
def repeating (mul,romNum) | |
romNum + ("#{romNum}" * (mul-1)) | |
end | |
def divideby500 (num) | |
value = num | |
div1 = value.divmod(500) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"D") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby100 (div1[1]) | |
end | |
elsif div1[0] == 0 && div1[1] > 0 | |
divideby100 (div1[1]) | |
end | |
end | |
def divideby100 (num) | |
value = num | |
div1 = value.divmod(100) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"C") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby50 (div1[1]) | |
end | |
elsif div1[0] ==0 && div1[1] > 0 | |
divideby50 (div1[1]) | |
end | |
end | |
def divideby50 (num) | |
value = num | |
div1 = value.divmod(50) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"L") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby10 (div1[1]) | |
end | |
elsif div1[0] == 0 && div1[1] > 0 | |
divideby10 (div1[1]) | |
end | |
end | |
def divideby10 (num) | |
value = num | |
div1 = value.divmod(10) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"X") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby5 (div1[1]) | |
end | |
elsif div1[0] == 0 && div1[1] > 0 | |
divideby5 (div1[1]) | |
end | |
end | |
def divideby5 (num) | |
value = num | |
div1 = value.divmod(5) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"V") | |
@arrayOfRomanNumerals << result | |
if div1[1] > 0 | |
divideby1 (div1[1]) | |
end | |
elsif div1[0] == 0 && div1[1] > 0 | |
divideby1 (div1[1]) | |
end | |
end | |
def divideby1 (num) | |
value = num | |
div1 = value.divmod(1) | |
if div1[0] > 0 | |
mul1 = div1[0] | |
result=repeating(mul1,"I") | |
@arrayOfRomanNumerals << result | |
elsif div1[0] == 0 && div1[1] > 0 | |
end | |
end | |
puts "please write a Whole number between 1- 9,999 to see the roman numerals" | |
num =gets.chomp.to_i | |
to_roman(num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment