School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,982 people online right now. Registration is fast and FREE... Join Now!




Roman numerals (olde style)

 

Roman numerals (olde style)

Inkman

14 Oct, 2008 - 12:15 PM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
Ok, I got to where it will take the numbers, if I put is the exact numbers I get the letters.

I just don't know what I need to make it add all the numbers up.

So if I want to put 4 I get "IIII" or 3102 I get "MXII".

Here is what I've got.

CODE
roman = gets.chomp.to_i

if roman == 1
  puts 'I'
    elsif roman == 5
      puts 'V'
        elsif roman == 10
          puts 'X'  
            elsif roman == 50
              puts 'L'  
                elsif roman == 100
                  puts 'C'            
                    elsif roman == 500
                      puts 'D'
                        elsif roman == 1000
                          puts 'M'
                            

puts roman.to_i
end


User is offlineProfile CardPM
+Quote Post


RandCode

RE: Roman Numerals (olde Style)

23 Oct, 2008 - 08:19 AM
Post #2

New D.I.C Head
*

Joined: 23 Oct, 2008
Posts: 7


My Contributions
In my opinion, that script, as is, will never say a good translation between numbers and romans ones.
In the way you wrote it, the program, does what you write, it sees a 4 and it uses the I cause you didn't made it understand the exceptions, like IV, cause, it says: if you see a 5, write V, but, even the 4 needs the V.

I'm starting with Ruby so I can't give you a code answer yet, but this is my hint:
Split the number starting from the big to small like:

3102 is divisible for 1000? get the integer of that and go on taking off the 3 from the number
102 is divisible for 500? no, so go for the 100. How many? 1 and take it off.
2 is divisible for 5 go for the 1.

In this way is a more checked way to do it. not perfect, but better.

Another hint to make it accettable, make it check the numbers 4, and 9 to make it IV IX and test it again.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:52PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month