Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Comments on Integer to Roman numeral

Post

Integer to Roman numeral

+3
−0

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 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

#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));
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
General comments
celtschk‭ wrote almost 4 years ago

This is not kolmogorov-complexity, as it takes input.

trichoplax‭ wrote over 3 years ago

Rather than depend only on an external link, could the rules also be defined in the challenge wording so it's self contained and consistent in the event of any changes to external sites?

Lundin‭ wrote over 3 years ago

@trichoplax. This one already went live here. And we still need a way to formalize moving things from sandbox to live challenge... guess I should mark it as "finalized" like some people do. As for the link, Roman numbers should be common knowledge really.