From: Derek Teixeira Date: 2007-02-19T02:31:14+09:00 Subject: Re: modulus/division.. Timothy Hunter wrote: > Derek Teixeira wrote: >> puts 4%5 >> >> and for everyline i got "4" as the remainder.. but when i tried these >> with a calulator .. i only got 4 as a remainder for the 1000. when i did >> the 4/500 i got a .008 .. so shouldn't the % be an 8? >> >> > When I was in elementary school they taught us these words for division. > Suppose you have 9 divided by 4. 4 is the "divisor". 9 is the > "dividend". 4 goes into 9 2 times, so 2 is the "quotient", but since 4 > times 2 is only 8, there is 1 left over. 1 is called the "remainder". > The modulus operator returns the remainder after dividing two integers. > That is, 9 % 4 = 1. > > So what is 4 % 5? 5 goes into 4 0 times. 0 times 5 is 0, so there is 4 > left over. 4 % 5 = 4. For 4 % 10, 10 goes into 4 0 times, with 4 left > over. 4 % 10 = 4. Same for 4 % 50 and all your other examples. > > For interesting experiments, use a divisor that is smaller than the > dividend. 10 % 4 = 2. 12 % 3 = 0. > > For more interesting experiments, use negative numbers. i think i've got it! is this pretty mcuh what they are saying .. i put explanations next to each line .. def old_roman_number 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 4/1000 means 1000 goes into 4 zero times so 0*1000 = 0 .. so 4-0= 4 d_mod = input%500 4/500 means 500 goes into 4 zero times 0* 500 = 0 ... so 4-0=4 c_mod = input%100 4/100 means 100 goes into 4 zero times 0*100 =0 .. so 4-0=4 l_mod = input%50 4/50 means 50 goes into 4 zero times 0*50 = 0 .. so 4-0=4 x_mod = input%10 4/10 means 10 goes into 4 zero times 0*10 = 0 .. so 4-0 = 4 v_mod = input%5 4/5 means 5 goes into 4 zero time 0*5 = 0 .. so 4-0= 4 m_div = input/1000 = 4/1000 = 0 d_div = m_mod/500 = 4/500 = 0 c_div = d_mod/100 = 4/100 = 0 l_div = c_mod/50 = 4/50 = 0 x_div = l_mod/10 = 4/10 = 0 v_div = x_mod/5 = 4/5 = 0 i_div = v_mod/1 = 4/1=4 m = 'M' * m_div = 0 d = 'D' * d_div = 0 c = 'C' * c_div = 0 l = 'L' * l_div = 0 x = 'X' * x_div = 0 v = 'V' * v_div = 0 i = 'I' * i_div = 4 * I equals IIII puts m + d + c + l + x + v + i end number = gets.chomp.to_i old_roman_number(number) -- Posted via http://www.ruby-forum.com/.