Post History
The task is to take a decimal integer as input and print the corresponding Roman numeral with capital letters. The program must handle all positive integer numbers between 1 and 1000. Input can be...
Question
code-golf
#2: Post edited
The task is to take a decimal integer as input and print the corresponding [Roman numeral](https://en.wikipedia.org/wiki/Roman_numerals) with capital letters. The program must handle all positive integer numbers between 1 and 1000. Input can be assumed to be correct and no error handling is necessary. Example data: ``` Input Output 1 I 8 VIII 9 IX 10 X 44 XLIV 666 DCLXVI 800 DCCC 888 DCCCLXXXVIII 900 CM 999 CMXCIX 1000 M ``` This is code golf, shortest source code wins. --- Example code of a simple iterative algorithm in C ```c #include <stdio.h> #include <string.h> char* romanize (int val) { static char result[99]=""; typedef struct { int val; char* str; } roman_t; const roman_t lookup[] = { { 1, "I" }, { 4, "IV" }, { 5, "V" }, { 9, "IX" }, { 10, "X" }, { 40, "XL" }, { 50, "L" }, { 90, "XC" }, { 100, "C" }, { 400, "CD" }, { 500, "D", }, { 900, "CM" }, { 1000, "M" } }; const size_t supported_items = sizeof lookup / sizeof *lookup; for(size_t i=supported_items-1; i>=0 && val!=0; i--) { while(val >= lookup[i].val) { strcat(result, lookup[i].str); val -= lookup[i].val; } } return result; } int main (void) { int input; scanf("%d", &input); puts(romanize(input)); } ```
#1: Initial revision
Integer to Roman numeral
The task is to take a decimal integer as input and print the corresponding [Roman numeral](https://en.wikipedia.org/wiki/Roman_numerals) with capital letters. The program must handle all positive integer numbers between 1 and 1000. Input can be assumed to be correct and no error handling is necessary. Example data: ``` Input Output 1 I 8 VIII 9 IX 10 X 44 XLIV 666 DCLXVI 800 DCCC 888 DCCCLXXXVIII 900 CM 999 CMXCIX 1000 M ``` This is code golf, shortest source code wins. --- Example code of a simple iterative algorithm in C ```c #include <stdio.h> #include <string.h> char* romanize (int val) { static char result[99]=""; typedef struct { int val; char* str; } roman_t; const roman_t lookup[] = { { 1, "I" }, { 4, "IV" }, { 5, "V" }, { 9, "IX" }, { 10, "X" }, { 40, "XL" }, { 50, "L" }, { 90, "XC" }, { 100, "C" }, { 400, "CD" }, { 500, "D", }, { 900, "CM" }, { 1000, "M" } }; const size_t supported_items = sizeof lookup / sizeof *lookup; for(size_t i=supported_items-1; i>=0 && val!=0; i--) { while(val >= lookup[i].val) { strcat(result, lookup[i].str); val -= lookup[i].val; } } return result; } int main (void) { int input; scanf("%d", &input); puts(romanize(input)); } ```