For hundreds, tens and units the rule is pretty much the same, i.e. you have a 1, 5 and 10 character the representation will be the same for each, just that the letters change.
You could have a table of 10 entries which represents a template
0 -
1 = U
2 = UU
3 = UUU
4 = UF
5 = F
6 = FU
7 = FUU
8 = FUUU
9 = UT
Now also have for units, tens and hundreds your table:
Units = IVX
Tens = XLC
Hundreds = CDM
Apply your template for the number to the letter representation so a U is replaced by the first character, an F by the second and a T by the 3rd.
Thousands are just an M for each thousand.
Build your string starting with the thousands, then the hundreds, then the tens and then the units.
If you were building it backwards of course you could start with the units modding by 10, then construct your units string, divide by 10 and mod again and shift to the tens string, repeat with the hundreds string and when you get to the thousands string you will know it by the fact your string has just one character i.e. an M.