# Simple reusable solution

• The following solution makes the extension to numbers bigger than 3999 trivial. Since the logic behind roman numerals has been coded (rather than hardcoding digits!), in order to extend it one should only add the symbols to the table.

``````char[][] romanNumerals = new char[][]{
{'I', 'V'}, // Units
{'X', 'L'}, // Tens
{'C', 'D'}, // Hundreds
{'M', '\0'}, // Thousands
};
int currentPosition = 0;
StringBuilder res = new StringBuilder();
do{
int digit = num % 10;
if(1 <= digit && digit <= 3) {
while(digit-- > 0) res.insert(0, romanNumerals[currentPosition][0]);
}else if(digit == 4) {
res.insert(0, romanNumerals[currentPosition][1]);
res.insert(0, romanNumerals[currentPosition][0]);
}else if(5 <= digit && digit <= 8) {
while(digit-- > 5) res.insert(0, romanNumerals[currentPosition][0]);
res.insert(0, romanNumerals[currentPosition][1]);
}else if(digit == 9){
res.insert(0, romanNumerals[currentPosition+1][0]);
res.insert(0, romanNumerals[currentPosition][0]);
}
currentPosition++;
num /= 10;
}while(num > 0);

return res.toString();
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.