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

Dashboard
Notifications
Mark all as read
Challenges

Integer to Roman numeral

+5
−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));
}
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comments

3 answers

+4
−0

JavaScript (Node.js), 147 bytes

f=(a,b='IVXLCDM',g=Math.log10(a)<<1,c=10**(g/2),e=a/c|0,i=b[g+1],j=a=>b[g].repeat(a))=>a?(e<4?j(e):e<6?j(5-e)+i:e<9?i+j(e-5):j(1)+b[g+2])+f(a%c):''

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+0
−0

C (compliant), 197 198 bytes.

Golfed version of the function in the question, using X macros:

#define L Y(1000,M)Y(900,CM)Y(500,D)Y(400,CD)Y(100,C)Y(90,XC)Y(50,L)Y(40,XL)Y(10,X)Y(9,IX)Y(5,V)Y(4,IV)Y(1,I)return r;        
#define Y(n,R)for(;v>=n;v-=n)strcat(r,#R);
char r[99];char*f(int v){L}

This function abuses static initialization so it can only be called once. By adding *r=0;, we can call it multiple times during testing, which is why the code in the TIO link below looks slightly longer.

Try it online!


It can be minimized a bit further with non-compliant gcc tricks.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment

Amusingly, this is "Y macros", not "X macros"... because X didn't sit well with Roman numerals. I got trouble in one version of the code that used X(10,X) :) Lundin‭ 4 months ago

+0
−0

Perl 5 -pMRoman, 10 bytes (17 if including command-line switches)

$_=Roman$_

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!