-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roman numerals
68 lines (65 loc) · 1.22 KB
/
Roman numerals
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def new_roman_num num
roman_num = ''
digit = num/1000
if digit > 0
roman_num += ('M' * digit)
num = num - (1000 * digit)
end
digit = num/100
if digit == 4
roman_num += 'CD'
num -= 400
digit -= 4
elsif digit == 9
roman_num += 'CM'
num -= 900
digit -= 900
end
if digit >= 5
roman_num += 'D'
num = num - 500
digit -= 5
end
if digit > 0
roman_num += ('C' * digit)
num = num - 100*digit
end
digit = num/10
if digit == 4
roman_num += 'XL'
num -= 40
digit -= 4
elsif digit == 9
roman_num += 'XC'
num -= 90
digit -= 9
end
if digit >= 5
roman_num += 'L'
num -= 50
digit -= 5
end
if digit > 0
roman_num +=('X' * digit)
num -= 10 * digit
end
if num == 4
roman_num += 'IV'
num -= 4
elsif num == 9
roman_num += 'IX'
num -= 9
end
if num >= 5
roman_num += 'V'
num -= 5
end
if num > 0
roman_num += ('I' * num)
end
return roman_num
end
puts 'enter an integer between 1 and 3,000:'
num1 = gets.chomp.to_i
print new_roman_num num1
print "\n"