Convert integers to Roman numerals using two parallel static const tables.
#include <stdio.h>
static const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
static const char *const symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
void print_roman(int n) {
printf("%d = ", n);
for (size_t i = 0; i < sizeof values / sizeof values[0]; i++)
while (n >= values[i]) {
fputs(symbols[i], stdout);
n -= values[i];
}
putchar('\n');
}
int main(void) {
print_roman(1994);
print_roman(2026);
print_roman(49);
return 0;
}
1994 = MCMXCIV 2026 = MMXXVI 49 = XLIX
Greedy conversion: repeatedly take the largest value that fits. Listing the subtractive pairs (900 = CM, 4 = IV …) in the table keeps the code trivial. The tables never change, so they are static const.