From: Jan_K Date: 2006-04-04T03:43:46+09:00 Subject: Re: Learn to Program, by Chris Pine Chapter 9, exercise 3 (page 76) Modern Roman numerals. Eventually, someone thought it would be terribly clever if putting a smaller number before a larger one meant you had to subtract the smaller one. As a result of this development, you must now suffer. Rewrite your previous method to return the new-style Roman numerals, so when someone calls roman_numeral 4, it should return 'IV'. Solution: ---------------------------------------------------------------- def roman_numeral input while input < 1 || input > 3999 puts 'Please enter a number between 1 and 3999' input = gets.chomp.to_i end m_mod = input%1000 d_mod = input%500 c_mod = input%100 l_mod = input%50 x_mod = input%10 v_mod = input%5 m_div = input/1000 d_div = m_mod/500 c_div = d_mod/100 l_div = c_mod/50 x_div = l_mod/10 v_div = x_mod/5 i_div = v_mod/1 m = 'M' * m_div d = 'D' * d_div c = 'C' * c_div l = 'L' * l_div x = 'X' * x_div v = 'V' * v_div i = 'I' * i_div if i == 'IIII' && v != 'V' i = 'IV' elsif i == 'IIII' v = 'IX' i = '' end if x == 'XXXX' && l != 'L' x = 'XL' elsif x == 'XXXX' l = 'XC' x = '' end if c == 'CCCC' && d != 'D' c = 'CD' elsif c == 'CCCC' d = 'CM' c = '' end puts m + d + c + l + x + v + i end number = gets.chomp.to_i roman_numeral(number) ----------------------------------------------------------------