Hello.

Could anyone tell me how to convert roman numbers to decimal numbers. Can anyone provide some example for it?

Thanks a lot.
regist1

Recommended Answers

All 15 Replies

I would be most grateful if you could post/write a code for me, please.
I really need it, either convert decimal to roman or roman to decimal.

Thanks.

commented: Lazy. -1

Yes, of course I accept that.. but I'm just learning C and I'm in a bad situation now (no time), so if someone could help me.. it will be great.

anyway, I'm trying to write it alone.. :(

Yes, of course I accept that.. but I'm just learning C and I'm in a bad situation now (no time), so if someone could help me.. it will be great.

anyway, I'm trying to write it alone.. :(

The best thing you could do is post what you have so far and state where your having problems....

well, my problem is to find algorithm.. could you help me at least with the way I can white it (in C), just algorithm, some advices please..

You are not the first person that has posted here to deal with this problem. Do a search on the forums.

Hint, break the number that is input into pieces, going from largest to smallest. You probably know those pieces well, or if you don't look the system up on Wikipedia, it's a nice article.


Hint, break the number that is input into pieces, going from largest to smallest. You probably know those pieces well, or if ...

Something like this? (decimal to roman)

while (number >= 1000)
{
     printf("M");
     number -= 1000;
}

But I still dont understand how it works..
for example, I've got 595 (DXCV?) and how can I get roman number from it? ..

I've seen stuff with people putting a bar over it for multiple thousands, so V (bar) = 5000.
With your example of 595, how many thousands are in it? none, remainder 595, how many 500s are in it, 1, remainder 95. So we have D. Here's where it's a little tricky, but seems they only put the C if it's 100 before 1000, so 950 is not LM, it's CML (50 above 100 before 1000). So for numbers less than 100 we put if it's 10 less and put the remainder on the other side. 95 is between 90 and 100 with a remainder of 5. So we get XCV.

So subtraction and modulus (%) are your friends along with making some if statements for the trickier exceptions.

Yes, I've got it.. convert from decimal to roman is okey, I just compare my input number(decimal) and then print it according to roman numbers.. - Thank you!

And what if I want it reversly.. my input will be a roman nubmer and I need to convert it into decimal.. Do I need some array for it (for M - 1000) or what?

Look for numbers out of their size order. You could use a switch statment for the conversion and then add or subtract as needed. For example MCM: C is less than M could be 1100, but look ahead to the next one M, ok so we know it's going to be 1000 + (1000-100) we know that we were 100 before 1000 so there isn't going to be any 500 (since adding a D would just make it 2400 or MMCD). Count the hundreds and subtract for any XC type situations.

Please, give some example in C.. I'm confused, sorry :/

You've come pretty far with the first part, why don't you see what you can come up with. You were right, you probably want to read the number into a char array. Like you said you probably want to use a switch block to match Roman numeral to regular number. You might put that into a function. Take an example, like MCMXLVIII which has a few of the complications we talked about and write that as a "worst case." Go through your idea step by step on a piece of paper. Code it. Then after you've got that down test it with some of the simpler cases to see if you've overgeneralized it.

You've come pretty far with the first part, why don't you see what you can come up with. You were right, you probably want to read the number into a char array. Like you said you probably want to use a switch block to match Roman numeral to regular number. You might put that into a function. Take an example, like MCMXLVIII which has a few of the complications we talked about and write that as a "worst case." Go through your idea step by step on a piece of paper. Code it. Then after you've got that down test it with some of the simpler cases to see if you've overgeneralized it.

I'm lost.. I have no idea

I'm lost.. I have no idea

I once did a conversion from roman to decimal using the following approach:

  • I created a function to translate every roman symbol: M, D, C, L, X, V, I to its decimal equivalent.
  • I let my program read the roman number into a character array (aka: c-string).
  • Then I used a loop to iterate through every 'character' of the roman number.
  • (inside the loop) You always get the decimal equivalent of the current character and the next one, you compare them to each other, if the next one is bigger, then subtract its value from the value inside the variable you're going to use to store the decimal equivalent of the roman number.
    If it's not, then you just add it.
  • Once you've implemented the previous things, you can (eventually) start thinking up a way to validate the input to avoid that the user inputs any invalid data.

Hope this helps.

commented: Well directed. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.