This is very much the same kind of program as getting the number of quarters, dimes, nickels, and pennies in some change.
Each Roman letter has an associated numeric value.
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
For example, 24 = 0*1000 + 0*500 + 0*100 + 0*50 + 2*10 + 0*5 + 4*1.
Getting rid of zero terms, that's:
4*1 + 2*10
And in Roman numerals, it is:
XXIIII
You now have a valid Roman number. If you wish, you can now do some search and replace on the string:
DCCCC --> CM
CCCC --> CD
LXXXX --> XC
XXXX --> XL
VIIII --> IX
IIII --> IV
The order in which you do that is important. Starting at the top and working our way down, the only match we find is IIII-->IV.
XXIIII --> XXIV
Tada!
To implement this, you'll need a lookup array for values and an associated Roman letter array:
1 2
|
int values[] = { 1000, 500, 100, 50, 10, 5, 1 };
char letters[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
|
You'll also want to have a string where you can put the letters you get from the number:
1 2
|
char roman[100] = {0};
int count = 0;
|
(To add a letter, append it to the string:
roman[count++] = a roman letter;
.)
You now have everything you need to write a loop to decompose your number into letters.
Hope this helps.