# Roman Numeral to Decimal : Interview Question

Folks,
I need some code or the algorithm to convert a roman numerical to the
corresponding decimal number ?

This is an interview question and not any homework.

Thanks,
H
AlgorithmsProgrammingC

Last Comment
Venabili
josgood

josgood

This C project from Sourceforge looks like it may help you
HukkaHua

Which C project from Sourceforge are you talking about ?

Thanks,
H
harfang

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
shanikawm

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
josgood

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Kent Olsen

Hi harfang,

Great start, but it's not that simple.

VIM is 6,000
IVM is 4,000

:)

Kent
Infinity08

>> VIM is 6,000
>> IVM is 4,000

That depends on the notation you use. The Romans themselves used MMMMMM resp. MMMM.
In the Middle Ages, A horizontal line was placed above VI resp. IV to denote times 1000.
I've never seen your notation though ... It's quite ambiguous, as it could mean 994 resp. 996.
Kent Olsen

Roman Numeral notation had its oddities.  Hence the shift to the decimal system.  (If people didn't have 10 fingers, I wonder what mathematics would look like today?)

I recall different instructors having different ideas on so-called corner cases.

One swore that IL was proper for 49, another swore that that was shorthand and the additive nature of Roman Numbers dictated that XLIX was the "true" notation.

I don't remember ever seeing 49 represented as XLIX, except in theory.  On the other hand, that does support the claim that VIM is RN for 6,000.

Kent
Infinity08

>> that does support the claim that VIM is RN for 6,000.

That would require multiplication, no ? 6 * 1000. That's what I've never seen in Roman numerals.
SOLUTION
Kent Olsen

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Infinity08

>> (And to math geeks, it's kind of interesting reading.)

It is indeed. Nice link.
SOLUTION
harfang

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
harfang

Kent,

Sorry, I left the browser open for too long, so my post was written before I saw your link (thank you for it, it is interesting!). I'm glad I wrote "I'm ready to believe [...]", in retrospect. Also, I was happy to read: "it's more likely that someone writing MXMI intends 1991, rather than 1010001", because that fits my experience: books written after 1753, erm mdccliii...

(°v°)
Kent Olsen

Hi harfang,

That has to be just about the most lucid, well thought out, and articulate follow-up post I've seen on this forum.

Can we bronze it?   :)

harfang

Thank you for your kind words! -- (^v°)
Arthur_Wood

"If people didn't have 10 fingers, I wonder what mathematics would look like today?"

If we only had 2 fingers, the everything would be in BINARY.

There is nothing special about base 10.  After all, many computers work in Hexa-decimal (base 16, 0123456789ABCDEF), or Binary (base 2, 01).

Just remember,

There are precisely 10 kinds of people in this world:

Those who understand Binary Numbers, and those who do not.

;-)

AW
Kent Olsen

Hi Arthur,

There's really two things at work here.  If people had 4 fingers instead of 5, our mathematics would likely be base 8, not decimal.  (Of course, base 10 would be just another abstraction so we'd likely call base 8, decimal.)

The other is that computers aren't really hexadecimal.  They're a collection of some number of binary digits.  A hex machine collects 4 bits.  An octal machine collects 3 bits.  And hex vs. octal is really a display function, based on how best to split a byte.

Now think about this.  If the early computer designers had discovered a 3-state entity instead of standardizing on the 2-state (binary) on/off, they'd have achieved a 50% performance gain by using the 3-state object.  If so, computers today would be a derivative of base 3.  A byte would be base 9, and a word would be 81 bits.

Instead, the biggest arguments were big-endian or little endian, and 1's complement or 2's complement.

Of course, they could still have argued both of them in addition to an 81 bit word, huh?  :)

Kent
Arthur_Wood

Actually, we would (and do) call base 8 Octal (Decimal is from the Latin word for 10, as Octal is from the Latin word for 8)

Of course, I know that computers only operate on bits (1s and 0s), I was just making a point about the use of different bases.

Of course, there is always the issue of the common use of base 12 numbers (eggs in a Dozen, hours of Day vs night), or base 60 - minutes in an hour, seconds in a minute, basis of the number of degrees in a circle.  So the argument could be made that the use of base 10, and the fact that we have 10 fingers is purely coincidental.

And how (or why) did the romans devise the I, V, X, L, C, D, M system in the first place?

AW

HukkaHua

Ok Guys may you please throw in some code as well which would work or give some
pseudo code /algorithm ...

Thanks
H
SOLUTION
Kent Olsen

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Jose Parrot

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
harfang

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
HukkaHua

I guess the solution posted by harfang is the one :)
But I do not know VB :(, but I am trying to follow the logic.

A C solution would have been best :)

Thanks for all the discussion and help.

H
harfang

Kdo found some c code samples in {http:#22069004} and said there's more where those came from. I find VB slightly annoying, but quite readable. Simply treat it as pseudo-code (the goto's are really just used to fake try/catch and for error handling; the returned error code is of course specific to Excel).

If you don't need to enforce some restrictions on "valid" numbers and don't expect medieval notations, the first post {http:#22065794} is all you need anyway. To obtain the "strict" version, set that flag to true and medieval to false and simplify.

Again, do look at some look-ahead parsers, just to make sure. You *were* looking for algorithms and not for a ready-made implementation, weren't you?

(°v°)
Infinity08

The information and following discussion is good, and should be kept in the PAQ db imo.

code/algorithms as asked in the question :

http:#22065794 (harfang)
http:#22065798 (shanikawm)
http:#22065867 (josgood)
http:#22085106 (harfang)

some posts of the discussion that followed that are directly related to the question (hence also interesting) :

http:#22069004 (Kdo)
http:#22069554 (harfang)
http:#22075374 (Kdo)
http:#22082514 (JoseParrot)
Venabili

One answer per person - the rule in Cleanup is equal number of points per user in the splits.
Infinity08

There were 5 persons in my proposal, so each would get 100, for example :

http:#22065794 (harfang)             33
http:#22065798 (shanikawm)       100
http:#22065867 (josgood)            100
http:#22085106 (harfang)             33

http:#22069004 (Kdo)                   50
http:#22069554 (harfang)             34
http:#22075374 (Kdo)                   50
http:#22082514 (JoseParrot)        100

It was just an idea ;)
Venabili

:) Yes but it cannot be done automatically. Every answer in the split will get the equal number of points so 1 per person is what we do. And opening the door for awarding uneven splits will make all CVs work even worse.

Venabili
Kent Olsen

I really enjoyed this thread and believe that it definitely belongs in the PAQ.

Split the points among the contributors.

harfang       - C (only average work?)  :)
josgood      - CV (Bonus points because I'm in a good mood.)
JoseParrot  - XCIX (He loses one point for writing VB.)
shanikawm - CI (He gets JoseParrot's lost point.)
Kdo             - VIM (what else?)  :)
Infinity08    - C*\$!@ (Sorry Infinity -- I ran out of cute sayings.....)

:)
harfang

Same here: it was good, and I learned a few things. If HukkaHua doesn't close, an even Split(Strict:=True, Medieval:=True, Rule:=eeStandardCV) among V participants is fine: D-point question, C assist points, but A grade nonetheless (CD points for us and CV points for Venabili...)!

(^v°)
Infinity08

>> Infinity08    - C*\$!@ (Sorry Infinity -- I ran out of cute sayings.....)

I'm not sure if my comments measure up to the others enough to warrant points, but thanks for the thought :)

C*\$!@ is modern Roman for 25 million I'm sure ? ;)
Infinity08

@harfang : how can one top that witty reply heh
Venabili

>>I really enjoyed this thread
This particular thread was quite enjoyable read compared with most of the things I read today. Although I just run the cleanup in Math and Science and the threads there are usually a pleasure to deal with. Thanks everyone for responding. I wish all experts were so ready to help when I ping :)
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY