1. ## Roman Numeral Counter

Hello,

I am in a C programming class, and my instructor wants us to write a program that will count from 1 to 100, and print out the roman numeral equivalent to each number.

The exact wording of the question:

Write a program that prints a table of all the Roman numeral eqivalents of the decimal numbers in the range of 1 to 100

I don't even know where to start.
the chart should look like

Number Roman Numeral
----------------------------------------------------------------
1 I
2 II

I can get the chart to print by hard coding

Code:
```int main (void)
{

int rom;

printf ("Decimal    Roman Numeral\n");
printf ("-----------------------------\n");

for (rom = 0; rom <= 100; rom= rom +1)
{

if (rom >= 1 && rom < 4)
{
if (rom == 1)
printf ("%d                I\n\n", rom);

else
if (rom == 2)
printf ("%d               II\n\n", rom);

else
if (rom == 3)
printf ("%d               III\n\n", rom);

}//end if statment

else
if (rom >=4 && rom < 5)
printf ("%d               IV\n\n", rom);

else
if (rom >= 5 && rom <= 9)
{

if (rom == 5)
printf ("%d                V\n\n", rom);

else
if (rom == 6)
printf ("%d               VI\n\n", rom);

else
if (rom == 7)
printf ("%d              VII\n\n", rom);

else
if (rom == 8)
printf ("%d              VIII\n\n", rom);

else
if (rom == 9)
printf ("%d              IX\n\n", rom);

}//end if statment

}// end for

printf ("\n\n");

return 0;
}```

I am just afraid that this is too much hard coding for her. Is there an easier way to achieve that same results? 2. Originally Posted by luigiramez12 Is there an easier way to achieve that same results?
Yes. Since it's a C programming class, your instructor probably wants you to *program* the table, not hard code it.

You're being asked to figure out an algorithm for converting from decimal to roman numerals.

If you have no idea where to start on such an algorithm, there are examples on the web if you search. 3. Probably. The task is to write a program... for generating Roman Numerals.
Your first attempt looks more like a table from 1 to 100 with all the results hard-coded.
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500 (you won't need for numbers 1 - 100)
M = 1000 (you won't need for numbers 1 - 100)

It's been a few years since I wrote one of these. Try to do a few examples by hand. Say the nuumer 43... What do you do? Try a "L"? Too high? Try a bunch of "X"s? More than three? Etc. Eventually you'll arrive at a method. 4. The only thing I can find is how to do it in C++ when I search for it in google! 5. Well I'm sure your teacher doesn't want you to simply find the program. As a programmer, you are expected to devise the solution on your own. Using plus, minus, times, divide, decisions, etc. 6. Originally Posted by luigiramez12 The only thing I can find is how to do it in C++ when I search for it in google!
Roman numerals have strict rules about how they're printed ( e.g. you can write "VIII" or "IX" or "XVI" but can you write "IXV"? )
Maybe you should search a little about that too. 7. Originally Posted by luigiramez12 The only thing I can find is how to do it in C++ when I search for it in google!
Programming trick # 46:

There is an association in many problems involving numbers or letters, that can be exploited, because the numbers and letters are consecutive.

They are consecutive.

You can associate an int array with a char array, using their index number, in different arrays.

(number is an int array, word is a 2d char array: word[][]
then you can make:

number = word = "zero"
number = word = "one"
etc.

Your problem is different, but this trick can be used to solve it, and many others.

Your fears are fully justified - take a fresh start. 8. Learn about 'lookup tables', or at the very least 'switch statements'. You should use that instead of a whole lot of it-then-else cases.
Using a lookup table for each decimal digit in the input number is a reasonable way to go. 9. A lookup table for representing Roman digit groups for decimal '0' to '9', depending on their positioning as units, tens, hundreds would be one way.

Or make a loop to replicate 'I's, even after a "V"... I, II, III vs. VI, VII, VIII. Special exception for IV, IX. Then use lookup / indexing into a string depending on whether 'I' and 'V' should be scaled to 'X' and 'L' respectively. That way you're not duplicating a lot of code.

It’s a fun little program actually. 10. Ok, it ain't C but, this would be plagiarism and tut-tut! Here's a solution using Tcl. It might give some insight.

Code:
```proc roman:numeral {i} {
set res ""
foreach {value roman} {
1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I} {
while {\$i>=\$value} {
append res \$roman
incr i -\$value
}
}
set res
}``` 11. Originally Posted by nonoob A lookup table for representing Roman digit groups for decimal '0' to '9', depending on their positioning as units, tens, hundreds would be one way.

Or make a loop to replicate 'I's, even after a "V"... I, II, III vs. VI, VII, VIII. Special exception for IV, IX. Then use lookup / indexing into a string depending on whether 'I' and 'V' should be scaled to 'X' and 'L' respectively. That way you're not duplicating a lot of code.

Its a fun little program actually.
The big problem with roman numerals is that they are not positional math.
Beyond that there are certain combinations where you subtract rather than add.
Code:
```I        = 1
II       = 2
III      = 3
IV       = 4      <--- 5 - 1
V        = 5      <--- back to single digit```
The rules are complex as all get out...

Roman numerals - Wikipedia, the free encyclopedia 12. Not that complicated. Although there are wrinkles I was not aware of...

Such as you can't use 'I' as a subtractor to anything but its immediate next decade. "IX" is OK, but "IC" is not for 99, nor are you allowed "IM" for 999. And you are not allowed to use any of the fiver units "V", "L", "D" as subtractors. Otherwise 95 could be written as "VC" and 1995 could be "MVM".

WJG, the Tcl algorithm is devilishly clever. Popular pages Recent additions 