Roman numerals/Encode: Difference between revisions

Content added Content deleted
m (→‎{{header|Tailspin}}: update to stricter typing)
m (syntax highlighting fixup automation)
Line 16: Line 16:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
<syntaxhighlight lang="11l">V anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
V rnums = ‘M CM D CD C XC L XL X IX V IV I’.split(‘ ’)
V rnums = ‘M CM D CD C XC L XL X IX V IV I’.split(‘ ’)


Line 30: Line 30:
1009, 1444, 1666, 1945, 1997, 1999, 2000, 2008, 2010, 2011, 2500, 3000, 3999]
1009, 1444, 1666, 1945, 1997, 1999, 2000, 2008, 2010, 2011, 2500, 3000, 3999]
L(val) test
L(val) test
print(val‘ - ’to_roman(val))</lang>
print(val‘ - ’to_roman(val))</syntaxhighlight>


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==
<lang 8080asm> org 100h
<syntaxhighlight lang="8080asm"> org 100h
jmp test
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 131: Line 131:
dgtbufdef: db 5,0
dgtbufdef: db 5,0
dgtbuf: ds 6
dgtbuf: ds 6
romanbuf:</lang>
romanbuf:</syntaxhighlight>
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
===Main and Supporting Functions===
===Main and Supporting Functions===
The main program and test values: 70,1776,2021,3999,4000
The main program and test values: 70,1776,2021,3999,4000
<lang asm> mov ax,0070h
<syntaxhighlight lang="asm"> mov ax,0070h
call EncodeRoman
call EncodeRoman
mov si,offset StringRam
mov si,offset StringRam
Line 164: Line 164:


ReturnToDos ;macro that calls the int that exits dos</lang>
ReturnToDos ;macro that calls the int that exits dos</syntaxhighlight>


The <code>EncodeRoman</code> routine:
The <code>EncodeRoman</code> routine:
<lang asm>;ROMAN NUMERALS MODULE
<syntaxhighlight lang="asm">;ROMAN NUMERALS MODULE


EncodeRoman:
EncodeRoman:
Line 340: Line 340:
ror al,cl ;AX = 0X0Yh
ror al,cl ;AX = 0X0Yh
pop cx
pop cx
ret</lang>
ret</syntaxhighlight>


Macros used:
Macros used:
<lang asm>pushall macro
<syntaxhighlight lang="asm">pushall macro
push ax
push ax
push bx
push bx
Line 362: Line 362:
pop bx
pop bx
pop ax
pop ax
endm</lang>
endm</syntaxhighlight>
===Output===
===Output===
{{out}}
{{out}}
Line 374: Line 374:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>DEFINE PTR="CARD"
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
CARD ARRAY arabic=[1000 900 500 400 100 90 50 40 10 9 5 4 1]
CARD ARRAY arabic=[1000 900 500 400 100 90 50 40 10 9 5 4 1]
PTR ARRAY roman(13)
PTR ARRAY roman(13)
Line 413: Line 413:
PrintF("%U=%S%E",data(i),r)
PrintF("%U=%S%E",data(i),r)
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Roman_numerals_encode.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Roman_numerals_encode.png Screenshot from Atari 8-bit computer]
Line 426: Line 426:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang ActionScript>function arabic2roman(num:Number):String {
<syntaxhighlight lang="actionscript">function arabic2roman(num:Number):String {
var lookup:Object = {M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1};
var lookup:Object = {M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1};
var roman:String = "", i:String;
var roman:String = "", i:String;
Line 440: Line 440:
trace("2008 in roman is " + arabic2roman(2008));
trace("2008 in roman is " + arabic2roman(2008));
trace("1666 in roman is " + arabic2roman(1666));
trace("1666 in roman is " + arabic2roman(1666));
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1990 in roman is MCMXC
<pre>1990 in roman is MCMXC
Line 447: Line 447:
</pre>
</pre>
And the reverse:
And the reverse:
<lang ActionScript>function roman2arabic(roman:String):Number {
<syntaxhighlight lang="actionscript">function roman2arabic(roman:String):Number {
var romanArr:Array = roman.toUpperCase().split('');
var romanArr:Array = roman.toUpperCase().split('');
var lookup:Object = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
var lookup:Object = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
Line 459: Line 459:
trace("MCMXC in arabic is " + roman2arabic("MCMXC"));
trace("MCMXC in arabic is " + roman2arabic("MCMXC"));
trace("MMVIII in arabic is " + roman2arabic("MMVIII"));
trace("MMVIII in arabic is " + roman2arabic("MMVIII"));
trace("MDCLXVI in arabic is " + roman2arabic("MDCLXVI"));</lang>
trace("MDCLXVI in arabic is " + roman2arabic("MDCLXVI"));</syntaxhighlight>
{{out}}
{{out}}
<pre>MCMXC in arabic is 1990
<pre>MCMXC in arabic is 1990
Line 466: Line 466:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Roman_Numeral_Test is
procedure Roman_Numeral_Test is
Line 498: Line 498:
Put_Line (To_Roman (25));
Put_Line (To_Roman (25));
Put_Line (To_Roman (944));
Put_Line (To_Roman (944));
end Roman_Numeral_Test;</lang>
end Roman_Numeral_Test;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 512: Line 512:


{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<lang algol68>[]CHAR roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands #
<syntaxhighlight lang="algol68">[]CHAR roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands #
[]CHAR adjust roman = "CCXXmmccxxii";
[]CHAR adjust roman = "CCXXmmccxxii";
[]INT arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1);
[]INT arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1);
Line 540: Line 540:
print((val, " - ", arabic to roman(val), new line))
print((val, " - ", arabic to roman(val), new line))
OD
OD
)</lang>
)</syntaxhighlight>
{{out}} (last example is manually wrapped):
{{out}} (last example is manually wrapped):
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
Line 641: Line 641:
{{works with|awtoc|any - tested with release [http://www.jampan.co.nz/~glyn/aw2c.tar.gz Mon Apr 27 14:25:27 NZST 2009]}}
{{works with|awtoc|any - tested with release [http://www.jampan.co.nz/~glyn/aw2c.tar.gz Mon Apr 27 14:25:27 NZST 2009]}}
<!-- This specimen was emailed to be by Glyn Webster > "Here's a Roman number procedure that would fit in:" -->
<!-- This specimen was emailed to be by Glyn Webster > "Here's a Roman number procedure that would fit in:" -->
<lang algolw>BEGIN
<syntaxhighlight lang="algolw">BEGIN


PROCEDURE ROMAN (INTEGER VALUE NUMBER; STRING(15) RESULT CHARACTERS; INTEGER RESULT LENGTH);
PROCEDURE ROMAN (INTEGER VALUE NUMBER; STRING(15) RESULT CHARACTERS; INTEGER RESULT LENGTH);
Line 691: Line 691:
ROMAN(2009, S, I); WRITE(S, I);
ROMAN(2009, S, I); WRITE(S, I);
ROMAN(405, S, I); WRITE(S, I);
ROMAN(405, S, I); WRITE(S, I);
END.</lang>
END.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 703: Line 703:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<lang APL>toRoman←{
<syntaxhighlight lang="apl">toRoman←{
⍝ Digits and corresponding values
⍝ Digits and corresponding values
ds←((⊢≠⊃)⊆⊢)' M CM D CD C XC L XL X IX V IV I'
ds←((⊢≠⊃)⊆⊢)' M CM D CD C XC L XL X IX V IV I'
Line 712: Line 712:
(d⊃ds),∇⍵-d⊃vs ⍝ While one exists, add it and subtract from number
(d⊃ds),∇⍵-d⊃vs ⍝ While one exists, add it and subtract from number
}⍵
}⍵
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 724: Line 724:
{{Trans|Haskell}}
{{Trans|Haskell}}
(mapAccumL version)
(mapAccumL version)
<lang AppleScript>------------------ ROMAN INTEGER STRINGS -----------------
<syntaxhighlight lang="applescript">------------------ ROMAN INTEGER STRINGS -----------------


-- roman :: Int -> String
-- roman :: Int -> String
Line 860: Line 860:
missing value
missing value
end if
end if
end snd</lang>
end snd</syntaxhighlight>
{{Out}}
{{Out}}
<pre>{"MMXVI", "MCMXC", "MMVIII", "MM", "MDCLXVI"}</pre>
<pre>{"MMXVI", "MCMXC", "MMVIII", "MM", "MDCLXVI"}</pre>
Line 866: Line 866:
=={{header|Arturo}}==
=={{header|Arturo}}==
{{trans|Nim}}
{{trans|Nim}}
<lang rebol>nums: [[1000 "M"] [900 "CM"] [500 "D"] [400 "CD"] [100 "C"] [90 "XC"]
<syntaxhighlight lang="rebol">nums: [[1000 "M"] [900 "CM"] [500 "D"] [400 "CD"] [100 "C"] [90 "XC"]
[50 "L"] [40 "XL"] [10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"])
[50 "L"] [40 "XL"] [10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"])
Line 892: Line 892:
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500
3000 3999] 'n
3000 3999] 'n
-> print [n "->" toRoman n]</lang>
-> print [n "->" toRoman n]</syntaxhighlight>


{{out}}
{{out}}
Line 953: Line 953:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{trans|C++}}
{{trans|C++}}
<lang AutoHotkey>MsgBox % stor(444)
<syntaxhighlight lang="autohotkey">MsgBox % stor(444)


stor(value)
stor(value)
Line 980: Line 980:
}
}
Return result . "O"
Return result . "O"
}</lang>
}</syntaxhighlight>


=={{header|Autolisp}}==
=={{header|Autolisp}}==
<syntaxhighlight lang="autolisp">
<lang Autolisp>
(defun c:roman() (romanNumber (getint "\n Enter number > "))
(defun c:roman() (romanNumber (getint "\n Enter number > "))
(defun romanNumber (n / uni dec hun tho nstr strlist nlist rom)
(defun romanNumber (n / uni dec hun tho nstr strlist nlist rom)
Line 1,008: Line 1,008:
rom
rom
)
)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,018: Line 1,018:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ROMAN_NUMERALS_ENCODE.AWK
# syntax: GAWK -f ROMAN_NUMERALS_ENCODE.AWK
BEGIN {
BEGIN {
Line 1,045: Line 1,045:
return(roman1000[v] roman100[w] roman10[x] roman1[y])
return(roman1000[v] roman100[w] roman10[x] roman1[y])
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,055: Line 1,055:
=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang gwbasic> 1 N = 1990: GOSUB 5: PRINT N" = "V$
<syntaxhighlight lang="gwbasic"> 1 N = 1990: GOSUB 5: PRINT N" = "V$
2 N = 2008: GOSUB 5: PRINT N" = "V$
2 N = 2008: GOSUB 5: PRINT N" = "V$
3 N = 1666: GOSUB 5: PRINT N" = "V$;
3 N = 1666: GOSUB 5: PRINT N" = "V$;
Line 1,061: Line 1,061:
5 V = N:V$ = "": FOR I = 0 TO 12: FOR L = 1 TO 0 STEP 0:A = VAL ( MID$ ("1E3900500400100+90+50+40+10+09+05+04+01",I * 3 + 1,3))
5 V = N:V$ = "": FOR I = 0 TO 12: FOR L = 1 TO 0 STEP 0:A = VAL ( MID$ ("1E3900500400100+90+50+40+10+09+05+04+01",I * 3 + 1,3))
6 L = (V - A) > = 0:V$ = V$ + MID$ ("M.CMD.CDC.XCL.XLX.IXV.IVI",I * 2 + 1,(I - INT (I / 2) * 2 + 1) * L):V = V - A * L: NEXT L,I
6 L = (V - A) > = 0:V$ = V$ + MID$ ("M.CMD.CDC.XCL.XLX.IXV.IVI",I * 2 + 1,(I - INT (I / 2) * 2 + 1) * L):V = V - A * L: NEXT L,I
7 RETURN</lang>
7 RETURN</syntaxhighlight>


==={{header|ASIC}}===
==={{header|ASIC}}===
{{trans|DWScript}}
{{trans|DWScript}}
<lang basic>
<syntaxhighlight lang="basic">
REM Roman numerals/Encode
REM Roman numerals/Encode
DIM Weights(12)
DIM Weights(12)
Line 1,107: Line 1,107:
ExitToRoman:
ExitToRoman:
RETURN
RETURN
</syntaxhighlight>
</lang>


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
{{works with|Commodore BASIC|7.0}}
{{works with|Commodore BASIC|7.0}}
C-128 version:
C-128 version:
<lang basic>100 DIM RN$(12),NV(12)
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12)
110 FOR I=0 TO 12
110 FOR I=0 TO 12
120 : READ RN$(I), NV(I)
120 : READ RN$(I), NV(I)
Line 1,137: Line 1,137:
330 : LOOP
330 : LOOP
340 : PRINT RN$;CHR$(13)
340 : PRINT RN$;CHR$(13)
350 LOOP</lang>
350 LOOP</syntaxhighlight>


{{works with|Commodore BASIC|3.5}}
{{works with|Commodore BASIC|3.5}}
C-16/116/Plus-4 version (BASIC 3.5 has DO/LOOP but not BEGIN/BEND)
C-16/116/Plus-4 version (BASIC 3.5 has DO/LOOP but not BEGIN/BEND)
<lang basic>100 DIM RN$(12),NV(12)
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12)
110 FOR I=0 TO 12
110 FOR I=0 TO 12
120 : READ RN$(I), NV(I)
120 : READ RN$(I), NV(I)
Line 1,165: Line 1,165:
330 : LOOP
330 : LOOP
340 : PRINT RN$;CHR$(13)
340 : PRINT RN$;CHR$(13)
350 LOOP</lang>
350 LOOP</syntaxhighlight>


{{works with|Commodore BASIC|2.0}}
{{works with|Commodore BASIC|2.0}}
This version works on any Commodore, though the title banner should be adjusted to match the color and screen width of the particular machine.
This version works on any Commodore, though the title banner should be adjusted to match the color and screen width of the particular machine.
<lang basic>100 DIM RN$(12),NV(12)
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12)
110 FOR I=0 TO 12
110 FOR I=0 TO 12
120 : READ RN$(I), NV(I)
120 : READ RN$(I), NV(I)
Line 1,194: Line 1,194:
340 : PRINT RN$;CHR$(13)
340 : PRINT RN$;CHR$(13)
350 GOTO 210
350 GOTO 210
</syntaxhighlight>
</lang>


The output is the same for all the above versions:
The output is the same for all the above versions:
Line 1,215: Line 1,215:
==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
{{works with|FreeBASIC}}
{{works with|FreeBASIC}}
<lang freebasic>
<syntaxhighlight lang="freebasic">
DIM SHARED arabic(0 TO 12) AS Integer => {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
DIM SHARED arabic(0 TO 12) AS Integer => {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
DIM SHARED roman(0 TO 12) AS String*2 => {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"}
DIM SHARED roman(0 TO 12) AS String*2 => {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"}
Line 1,236: Line 1,236:
PRINT "1666 = "; toRoman(1666)
PRINT "1666 = "; toRoman(1666)
PRINT "3888 = "; toRoman(3888)
PRINT "3888 = "; toRoman(3888)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,246: Line 1,246:


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PROGRAM "Roman.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Roman.bas"
110 DO
110 DO
120 PRINT :INPUT PROMPT "Enter an arabic number: ":N
120 PRINT :INPUT PROMPT "Enter an arabic number: ":N
Line 1,269: Line 1,269:
310 END DEF
310 END DEF
320 DATA 1000,"M",900,"CM",500,"D",400,"CD",100,"C",90,"XC"
320 DATA 1000,"M",900,"CM",500,"D",400,"CD",100,"C",90,"XC"
330 DATA 50,"L",40,"XL",10,"X",9,"IX",5,"V",4,"IV",1,"I"</lang>
330 DATA 50,"L",40,"XL",10,"X",9,"IX",5,"V",4,"IV",1,"I"</syntaxhighlight>


==={{header|Nascom BASIC}}===
==={{header|Nascom BASIC}}===
{{trans|DWScript}}
{{trans|DWScript}}
{{works with|Nascom ROM BASIC|4.7}}
{{works with|Nascom ROM BASIC|4.7}}
<lang basic>
<syntaxhighlight lang="basic">
10 REM Roman numerals/Encode
10 REM Roman numerals/Encode
20 DIM WEIGHTS(12),SYMBOLS$(12)
20 DIM WEIGHTS(12),SYMBOLS$(12)
Line 1,303: Line 1,303:
580 GOTO 520
580 GOTO 520
590 RETURN
590 RETURN
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,312: Line 1,312:


==={{header|ZX Spectrum Basic}}===
==={{header|ZX Spectrum Basic}}===
<lang zxbasic> 10 DATA 1000,"M",900,"CM"
<syntaxhighlight lang="zxbasic"> 10 DATA 1000,"M",900,"CM"
20 DATA 500,"D",400,"CD"
20 DATA 500,"D",400,"CD"
30 DATA 100,"C",90,"XC"
30 DATA 100,"C",90,"XC"
Line 1,328: Line 1,328:
150 GO TO 120
150 GO TO 120
160 NEXT I
160 NEXT I
170 PRINT VALUE;"=";V$</lang>
170 PRINT VALUE;"=";V$</syntaxhighlight>


==={{header|BaCon}}===
==={{header|BaCon}}===
<lang bacon>OPTION BASE 1
<syntaxhighlight lang="bacon">OPTION BASE 1


GLOBAL roman$[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }
GLOBAL roman$[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }
Line 1,354: Line 1,354:
PRINT toroman$(2008)
PRINT toroman$(2008)
PRINT toroman$(1666)
PRINT toroman$(1666)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,364: Line 1,364:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{works with|BASIC256 }}
{{works with|BASIC256 }}
<lang basic256>
<syntaxhighlight lang="basic256">
print 1666+" = "+convert$(1666)
print 1666+" = "+convert$(1666)
print 2008+" = "+convert$(2008)
print 2008+" = "+convert$(2008)
Line 1,381: Line 1,381:
next i
next i
end function
end function
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,392: Line 1,392:
=={{header|Batch File}}==
=={{header|Batch File}}==
{{trans|BASIC}}
{{trans|BASIC}}
<lang dos>@echo off
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
setlocal enabledelayedexpansion


Line 1,423: Line 1,423:
set result=!result!!rom%a%!
set result=!result!!rom%a%!
set /a value-=!arab%a%!
set /a value-=!arab%a%!
goto add_val</lang>
goto add_val</syntaxhighlight>
{{Out}}
{{Out}}
<pre>2009 = MMIX
<pre>2009 = MMIX
Line 1,430: Line 1,430:


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> PRINT ;1999, FNroman(1999)
<syntaxhighlight lang="bbcbasic"> PRINT ;1999, FNroman(1999)
PRINT ;2012, FNroman(2012)
PRINT ;2012, FNroman(2012)
PRINT ;1666, FNroman(1666)
PRINT ;1666, FNroman(1666)
Line 1,447: Line 1,447:
ENDWHILE
ENDWHILE
NEXT
NEXT
= r$</lang>
= r$</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,457: Line 1,457:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let toroman(n, v) = valof
let toroman(n, v) = valof
Line 1,498: Line 1,498:
show(3888)
show(3888)
show(2021)
show(2021)
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre>1666 = MDCLXVI
<pre>1666 = MDCLXVI
Line 1,510: Line 1,510:
Reads the number to convert from standard input. No range validation is performed.
Reads the number to convert from standard input. No range validation is performed.


<lang befunge>&>0\0>00p:#v_$ >:#,_ $ @
<syntaxhighlight lang="befunge">&>0\0>00p:#v_$ >:#,_ $ @
4-v >5+#:/#<\55+%:5/\5%:
4-v >5+#:/#<\55+%:5/\5%:
vv_$9+00g+5g\00g8+>5g\00
vv_$9+00g+5g\00g8+>5g\00
g>\20p>:10p00g \#v _20gv
g>\20p>:10p00g \#v _20gv
> 2+ v^-1g01\g5+8<^ +9 _
> 2+ v^-1g01\g5+8<^ +9 _
IVXLCDM</lang>
IVXLCDM</syntaxhighlight>


{{out}}
{{out}}
Line 1,523: Line 1,523:
=={{header|BQN}}==
=={{header|BQN}}==
{{trans|APL}}
{{trans|APL}}
<lang BQN>⟨ToRoman⇐R⟩ ← {
<syntaxhighlight lang="bqn">⟨ToRoman⇐R⟩ ← {
ds ← 1↓¨(¯1+`⊏⊸=)⊸⊔" I IV V IX X XL L XC C CD D CM M"
ds ← 1↓¨(¯1+`⊏⊸=)⊸⊔" I IV V IX X XL L XC C CD D CM M"
vs ← 1e3∾˜ ⥊1‿4‿5‿9×⌜˜10⋆↕3
vs ← 1e3∾˜ ⥊1‿4‿5‿9×⌜˜10⋆↕3
Line 1,530: Line 1,530:
(⊑⟜ds∾·𝕊𝕩-⊑⟜vs) 1-˜⊑vs⍋𝕩
(⊑⟜ds∾·𝕊𝕩-⊑⟜vs) 1-˜⊑vs⍋𝕩
}
}
}</lang>
}</syntaxhighlight>
{{out|Example use}}
{{out|Example use}}
<lang> ToRoman¨ 1990‿2008‿1666‿2021
<syntaxhighlight lang="text"> ToRoman¨ 1990‿2008‿1666‿2021
⟨ "MCMXC" "MMVIII" "MDCLXVI" "MMXXI" ⟩</lang>
⟨ "MCMXC" "MMVIII" "MDCLXVI" "MMXXI" ⟩</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>( ( encode
<syntaxhighlight lang="bracmat">( ( encode
= indian roman cifr tenfoldroman letter tenfold
= indian roman cifr tenfoldroman letter tenfold
. !arg:#?indian
. !arg:#?indian
Line 1,584: Line 1,584:
)
)
)
)
);</lang>
);</syntaxhighlight>
{{out}}
{{out}}
<pre>1990 MCMXC
<pre>1990 MCMXC
Line 1,596: Line 1,596:
===Naive solution===
===Naive solution===
This solution is a smart but does not return the number written as a string.
This solution is a smart but does not return the number written as a string.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>




Line 1,620: Line 1,620:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Enter arabic number:
<pre>Enter arabic number:
Line 1,629: Line 1,629:
</pre>
</pre>
===Not thread-safe===
===Not thread-safe===
<lang C>#define _CRT_SECURE_NO_WARNINGS
<syntaxhighlight lang="c">#define _CRT_SECURE_NO_WARNINGS


#include <stdio.h>
#include <stdio.h>
Line 1,717: Line 1,717:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{Output}}
{{Output}}
<pre>Write given numbers as Roman numerals.
<pre>Write given numbers as Roman numerals.
Line 1,740: Line 1,740:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
class Program
class Program
{
{
Line 1,767: Line 1,767:
}
}
}
}
}</lang>
}</syntaxhighlight>


One-liner Mono REPL
One-liner Mono REPL
<lang csharp>
<syntaxhighlight lang="csharp">
Func<int, string> toRoman = (number) =>
Func<int, string> toRoman = (number) =>
new Dictionary<int, string>
new Dictionary<int, string>
Line 1,788: Line 1,788:
{1, "I"}
{1, "I"}
}.Aggregate(new string('I', number), (m, _) => m.Replace(new string('I', _.Key), _.Value));
}.Aggregate(new string('I', number), (m, _) => m.Replace(new string('I', _.Key), _.Value));
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,807: Line 1,807:
=={{header|C++}}==
=={{header|C++}}==
===C++ 98===
===C++ 98===
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>


Line 1,847: Line 1,847:
std::cout << to_roman(i) << std::endl;
std::cout << to_roman(i) << std::endl;
}
}
}</lang>
}</syntaxhighlight>


===C++ 11===
===C++ 11===
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>


Line 1,881: Line 1,881:
for (int i = 0; i < 2018; i++)
for (int i = 0; i < 2018; i++)
std::cout << i << " --> " << to_roman(i) << std::endl;
std::cout << i << " --> " << to_roman(i) << std::endl;
}</lang>
}</syntaxhighlight>


=={{header|Ceylon}}==
=={{header|Ceylon}}==
<lang ceylon>shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {
class Numeral(shared Character char, shared Integer int) {}
class Numeral(shared Character char, shared Integer int) {}
Line 1,928: Line 1,928:
assert (toRoman(1990) == "MCMXC");
assert (toRoman(1990) == "MCMXC");
assert (toRoman(2008) == "MMVIII");
assert (toRoman(2008) == "MMVIII");
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
The easiest way is to use the built-in cl-format function
The easiest way is to use the built-in cl-format function
<lang Clojure>(def arabic->roman
<syntaxhighlight lang="clojure">(def arabic->roman
(partial clojure.pprint/cl-format nil "~@R"))
(partial clojure.pprint/cl-format nil "~@R"))


Line 1,938: Line 1,938:
;"CXXIII"
;"CXXIII"
(arabic->roman 99)
(arabic->roman 99)
;"XCIX"</lang>Alternatively:<lang Clojure>(def roman-map
;"XCIX"</syntaxhighlight>Alternatively:<syntaxhighlight lang="clojure">(def roman-map
(sorted-map
(sorted-map
1 "I", 4 "IV", 5 "V", 9 "IX",
1 "I", 4 "IV", 5 "V", 9 "IX",
Line 1,954: Line 1,954:


(int->roman 1999)
(int->roman 1999)
; "MCMXCIX"</lang>
; "MCMXCIX"</syntaxhighlight>




An alternate implementation:
An alternate implementation:


<syntaxhighlight lang="clojure">
<lang Clojure>
(defn a2r [a]
(defn a2r [a]
(let [rv '(1000 500 100 50 10 5 1)
(let [rv '(1000 500 100 50 10 5 1)
Line 1,976: Line 1,976:
(and (< a v) (< a l)) (recur a (rest rv) (rest dv) r)
(and (< a v) (< a l)) (recur a (rest rv) (rest dv) r)
:else (recur (- a l) (rest rv) (rest dv) (str r (rm d) (rm v)))))))))
:else (recur (- a l) (rest rv) (rest dv) (str r (rm d) (rm v)))))))))
</syntaxhighlight>
</lang>


Usage:
Usage:


<syntaxhighlight lang="clojure">
<lang Clojure>
(a2r 1666)
(a2r 1666)
"MDCLXVI"
"MDCLXVI"
Line 1,986: Line 1,986:
(map a2r [1000 1 389 45])
(map a2r [1000 1 389 45])
("M" "I" "CCCLXXXIX" "XLV")
("M" "I" "CCCLXXXIX" "XLV")
</syntaxhighlight>
</lang>


An alternate implementation:
An alternate implementation:


<syntaxhighlight lang="clojure">
<lang Clojure>
(def roman-map
(def roman-map
(sorted-map-by >
(sorted-map-by >
Line 2,011: Line 2,011:
(>= v e) (cons roman (a2r v n))
(>= v e) (cons roman (a2r v n))
(< v e) (cons roman (a2r v (rest n))))))))
(< v e) (cons roman (a2r v (rest n))))))))
</syntaxhighlight>
</lang>


Usage:
Usage:


<syntaxhighlight lang="clojure">
<lang Clojure>
(a2r 1666)
(a2r 1666)
"MDCLXVI"
"MDCLXVI"
Line 2,021: Line 2,021:
(map a2r [1000 1 389 45])
(map a2r [1000 1 389 45])
("M" "I" "CCCLXXXIX" "XLV")
("M" "I" "CCCLXXXIX" "XLV")
</syntaxhighlight>
</lang>


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>roman = cluster is encode
<syntaxhighlight lang="clu">roman = cluster is encode
rep = null
rep = null
Line 2,070: Line 2,070:
stream$putl(po, int$unparse(test) || " = " || roman$encode(test))
stream$putl(po, int$unparse(test) || " = " || roman$encode(test))
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>1666 = MDCLXVI
<pre>1666 = MDCLXVI
Line 2,081: Line 2,081:
=={{header|COBOL}}==
=={{header|COBOL}}==


<syntaxhighlight lang="cobol">
<lang COBOL>
IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. TOROMAN.
PROGRAM-ID. TOROMAN.
Line 2,135: Line 2,135:
end-perform
end-perform
.
.
</syntaxhighlight>
</lang>
{{out}} (input was supplied via STDIN)
{{out}} (input was supplied via STDIN)
<pre>
<pre>
Line 2,155: Line 2,155:
=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==


<lang coffeescript>
<syntaxhighlight lang="coffeescript">
decimal_to_roman = (n) ->
decimal_to_roman = (n) ->
# This should work for any positive integer, although it
# This should work for any positive integer, although it
Line 2,202: Line 2,202:
else
else
console.log "error for #{decimal}: #{roman} is wrong"
console.log "error for #{decimal}: #{roman} is wrong"
</syntaxhighlight>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<lang lisp>(defun roman-numeral (n)
<syntaxhighlight lang="lisp">(defun roman-numeral (n)
(format nil "~@R" n))</lang>
(format nil "~@R" n))</syntaxhighlight>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "argv.coh";
include "argv.coh";


Line 2,267: Line 2,267:
print(decimalToRoman(number as uint16, &buffer as [uint8]));
print(decimalToRoman(number as uint16, &buffer as [uint8]));
print_nl();
print_nl();
end loop;</lang>
end loop;</syntaxhighlight>


{{out}}
{{out}}
Line 2,277: Line 2,277:


=={{header|D}}==
=={{header|D}}==
<lang d>string toRoman(int n) pure nothrow
<syntaxhighlight lang="d">string toRoman(int n) pure nothrow
in {
in {
assert(n < 5000);
assert(n < 5000);
Line 2,302: Line 2,302:
}
}


void main() {}</lang>
void main() {}</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==
{{trans|DWScript}}
{{trans|DWScript}}
<lang delphi>program RomanNumeralsEncode;
<syntaxhighlight lang="delphi">program RomanNumeralsEncode;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 2,333: Line 2,333:
Writeln(IntegerToRoman(2008)); // MMVIII
Writeln(IntegerToRoman(2008)); // MMVIII
Writeln(IntegerToRoman(1666)); // MDCLXVI
Writeln(IntegerToRoman(1666)); // MDCLXVI
end.</lang>
end.</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==
{{trans|D}}
{{trans|D}}
<lang delphi>const weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
<syntaxhighlight lang="delphi">const weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
const symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];


Line 2,356: Line 2,356:
PrintLn(toRoman(455));
PrintLn(toRoman(455));
PrintLn(toRoman(3456));
PrintLn(toRoman(3456));
PrintLn(toRoman(2488));</lang>
PrintLn(toRoman(2488));</syntaxhighlight>


=={{header|EasyLang}}==
=={{header|EasyLang}}==


<lang>values[] = [ 1000 900 500 400 100 90 50 40 10 9 5 4 1 ]
<syntaxhighlight lang="text">values[] = [ 1000 900 500 400 100 90 50 40 10 9 5 4 1 ]
symbol$[] = [ "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I" ]
symbol$[] = [ "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I" ]
func num2rom num . rom$ .
func num2rom num . rom$ .
Line 2,376: Line 2,376:
print r$
print r$
call num2rom 1666 r$
call num2rom 1666 r$
print r$</lang>
print r$</syntaxhighlight>


=={{header|ECL}}==
=={{header|ECL}}==
<lang ECL>RomanEncode(UNSIGNED Int) := FUNCTION
<syntaxhighlight lang="ecl">RomanEncode(UNSIGNED Int) := FUNCTION
SetWeights := [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
SetWeights := [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
SetSymbols := ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
SetSymbols := ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
Line 2,403: Line 2,403:
RomanEncode(1990 ); //MCMXC
RomanEncode(1990 ); //MCMXC
RomanEncode(2008 ); //MMVIII
RomanEncode(2008 ); //MMVIII
RomanEncode(1666); //MDCLXVI</lang>
RomanEncode(1666); //MDCLXVI</syntaxhighlight>


=={{header|Eiffel}}==
=={{header|Eiffel}}==
<lang Eiffel>class
<syntaxhighlight lang="eiffel">class
APPLICATION
APPLICATION


Line 2,464: Line 2,464:
Result := rnum
Result := rnum
end
end
end</lang>
end</syntaxhighlight>


=={{header|Ela}}==
=={{header|Ela}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang ela>open number string math
<syntaxhighlight lang="ela">open number string math


digit x y z k =
digit x y z k =
Line 2,483: Line 2,483:
| else = digit 'I' 'V' 'X' x
| else = digit 'I' 'V' 'X' x


map (join "" << toRoman) [1999,25,944]</lang>
map (join "" << toRoman) [1999,25,944]</syntaxhighlight>


{{out}}
{{out}}
Line 2,491: Line 2,491:
{{trans|C#}}
{{trans|C#}}
ELENA 5.0 :
ELENA 5.0 :
<lang elena>import system'collections;
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
import system'routines;
import extensions;
import extensions;
Line 2,522: Line 2,522:
console.printLine("2008 : ", 2008.toRoman());
console.printLine("2008 : ", 2008.toRoman());
console.printLine("1666 : ", 1666.toRoman())
console.printLine("1666 : ", 1666.toRoman())
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,532: Line 2,532:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Erlang}}
{{trans|Erlang}}
<lang elixir>defmodule Roman_numeral do
<syntaxhighlight lang="elixir">defmodule Roman_numeral do
def encode(0), do: ''
def encode(0), do: ''
def encode(x) when x >= 1000, do: [?M | encode(x - 1000)]
def encode(x) when x >= 1000, do: [?M | encode(x - 1000)]
Line 2,548: Line 2,548:
defp digit(8, x, y, _), do: [y, x, x, x]
defp digit(8, x, y, _), do: [y, x, x, x]
defp digit(9, x, _, z), do: [x, z]
defp digit(9, x, _, z), do: [x, z]
end</lang>
end</syntaxhighlight>


'''Another:'''
'''Another:'''
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule Roman_numeral do
<syntaxhighlight lang="elixir">defmodule Roman_numeral do
@symbols [ {1000, 'M'}, {900, 'CM'}, {500, 'D'}, {400, 'CD'}, {100, 'C'}, {90, 'XC'},
@symbols [ {1000, 'M'}, {900, 'CM'}, {500, 'D'}, {400, 'CD'}, {100, 'C'}, {90, 'XC'},
{50, 'L'}, {40, 'XL'}, {10, 'X'}, {9, 'IX'}, {5, 'V'}, {4, 'IV'}, {1, 'I'} ]
{50, 'L'}, {40, 'XL'}, {10, 'X'}, {9, 'IX'}, {5, 'V'}, {4, 'IV'}, {1, 'I'} ]
Line 2,561: Line 2,561:
Enum.join(roman)
Enum.join(roman)
end
end
end</lang>
end</syntaxhighlight>


'''Test:'''
'''Test:'''
<lang elixir>Enum.each([1990, 2008, 1666], fn n ->
<syntaxhighlight lang="elixir">Enum.each([1990, 2008, 1666], fn n ->
IO.puts "#{n}: #{Roman_numeral.encode(n)}"
IO.puts "#{n}: #{Roman_numeral.encode(n)}"
end)</lang>
end)</syntaxhighlight>


{{out}}
{{out}}
Line 2,576: Line 2,576:


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<lang lisp>(defun ar2ro (AN)
<syntaxhighlight lang="lisp">(defun ar2ro (AN)
"Translate from arabic number AN to roman number.
"Translate from arabic number AN to roman number.
For example, (ar2ro 1666) returns (M D C L X V I)."
For example, (ar2ro 1666) returns (M D C L X V I)."
Line 2,592: Line 2,592:
((>= AN 4) (cons 'I (cons 'V (ar2ro (- AN 4)))))
((>= AN 4) (cons 'I (cons 'V (ar2ro (- AN 4)))))
((>= AN 1) (cons 'I (ar2ro (- AN 1))))
((>= AN 1) (cons 'I (ar2ro (- AN 1))))
((= AN 0) nil)))</lang>
((= AN 0) nil)))</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
{{trans|OCaml}}
{{trans|OCaml}}
<lang erlang>-module(roman).
<syntaxhighlight lang="erlang">-module(roman).
-export([to_roman/1]).
-export([to_roman/1]).


Line 2,615: Line 2,615:
digit(7, X, Y, _) -> [Y, X, X];
digit(7, X, Y, _) -> [Y, X, X];
digit(8, X, Y, _) -> [Y, X, X, X];
digit(8, X, Y, _) -> [Y, X, X, X];
digit(9, X, _, Z) -> [X, Z].</lang>
digit(9, X, _, Z) -> [X, Z].</syntaxhighlight>


sample:
sample:
Line 2,630: Line 2,630:


Alternative:
Alternative:
<lang erlang>
<syntaxhighlight lang="erlang">
-module( roman_numerals ).
-module( roman_numerals ).


Line 2,651: Line 2,651:
map() -> [{"M",1000}, {"CM",900}, {"D",500}, {"CD",400}, {"C",100}, {"XC",90}, {"L",50}, {"XL",40}, {"X",10}, {"IX",9}, {"V",5}, {"IV",4}, {"I\
map() -> [{"M",1000}, {"CM",900}, {"D",500}, {"CD",400}, {"C",100}, {"XC",90}, {"L",50}, {"XL",40}, {"X",10}, {"IX",9}, {"V",5}, {"IV",4}, {"I\
",1}].
",1}].
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,664: Line 2,664:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM ARAB2ROMAN
PROGRAM ARAB2ROMAN


Line 2,690: Line 2,690:
TOROMAN(3888->ANS$) PRINT("3888 = ";ANS$)
TOROMAN(3888->ANS$) PRINT("3888 = ";ANS$)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|Euphoria}}==
=={{header|Euphoria}}==
{{trans|BASIC}}
{{trans|BASIC}}
<lang Euphoria>constant arabic = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
<syntaxhighlight lang="euphoria">constant arabic = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
constant roman = {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"}
constant roman = {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"}


Line 2,711: Line 2,711:
printf(1,"%d = %s\n",{2009,toRoman(2009)})
printf(1,"%d = %s\n",{2009,toRoman(2009)})
printf(1,"%d = %s\n",{1666,toRoman(1666)})
printf(1,"%d = %s\n",{1666,toRoman(1666)})
printf(1,"%d = %s\n",{3888,toRoman(3888)})</lang>
printf(1,"%d = %s\n",{3888,toRoman(3888)})</syntaxhighlight>


{{out}}
{{out}}
Line 2,723: Line 2,723:
Excel can encode numbers in Roman forms in 5 successively concise forms.
Excel can encode numbers in Roman forms in 5 successively concise forms.
These can be indicated from 0 to 4. Type in a cell:
These can be indicated from 0 to 4. Type in a cell:
<syntaxhighlight lang="excel">
<lang Excel>
=ROMAN(2013,0)
=ROMAN(2013,0)
</syntaxhighlight>
</lang>


It becomes:
It becomes:
<lang>
<syntaxhighlight lang="text">
MMXIII
MMXIII
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>let digit x y z = function
<syntaxhighlight lang="fsharp">let digit x y z = function
1 -> x
1 -> x
| 2 -> x + x
| 2 -> x + x
Line 2,760: Line 2,760:
|> List.map (fun n -> roman n)
|> List.map (fun n -> roman n)
|> List.iter (printfn "%s")
|> List.iter (printfn "%s")
0</lang>
0</syntaxhighlight>
{{out}}
{{out}}
<pre>MCMXC
<pre>MCMXC
Line 2,768: Line 2,768:
=={{header|Factor}}==
=={{header|Factor}}==
A roman numeral library ships with Factor.
A roman numeral library ships with Factor.
<lang factor>USE: roman
<syntaxhighlight lang="factor">USE: roman
( scratchpad ) 3333 >roman .
( scratchpad ) 3333 >roman .
"mmmcccxxxiii"</lang>
"mmmcccxxxiii"</syntaxhighlight>


Parts of the implementation:
Parts of the implementation:


<lang factor>CONSTANT: roman-digits
<syntaxhighlight lang="factor">CONSTANT: roman-digits
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" }
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" }


Line 2,789: Line 2,789:
roman-values roman-digits [
roman-values roman-digits [
[ /mod swap ] dip <repetition> concat
[ /mod swap ] dip <repetition> concat
] 2map "" concat-as nip ;</lang>
] 2map "" concat-as nip ;</syntaxhighlight>


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>^$." "
<syntaxhighlight lang="false">^$." "
[$999>][1000- "M"]#
[$999>][1000- "M"]#
$899> [ 900-"CM"]?
$899> [ 900-"CM"]?
Line 2,805: Line 2,805:
$ 4> [ 5- "V"]?
$ 4> [ 5- "V"]?
$ 3> [ 4-"IV"]?
$ 3> [ 4-"IV"]?
[$ ][ 1- "I"]#%</lang>
[$ ][ 1- "I"]#%</syntaxhighlight>


=={{header|Fan}}==
=={{header|Fan}}==
<syntaxhighlight lang="fan">**
<lang Fan>**
** converts a number to its roman numeral representation
** converts a number to its roman numeral representation
**
**
Line 2,845: Line 2,845:
}
}


}</lang>
}</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: vector create ( n -- ) 0 do , loop does> ( n -- ) swap cells + @ execute ;
<syntaxhighlight lang="forth">: vector create ( n -- ) 0 do , loop does> ( n -- ) swap cells + @ execute ;
\ these are ( numerals -- numerals )
\ these are ( numerals -- numerals )
: ,I dup c@ C, ; : ,V dup 1 + c@ C, ; : ,X dup 2 + c@ C, ;
: ,I dup c@ C, ; : ,V dup 1 + c@ C, ; : ,X dup 2 + c@ C, ;
Line 2,865: Line 2,865:
1999 roman type \ MCMXCIX
1999 roman type \ MCMXCIX
25 roman type \ XXV
25 roman type \ XXV
944 roman type \ CMXLIV</lang>
944 roman type \ CMXLIV</syntaxhighlight>
Alternative implementation
Alternative implementation
<lang forth>create romans 0 , 1 , 5 , 21 , 9 , 2 , 6 , 22 , 86 , 13 ,
<syntaxhighlight lang="forth">create romans 0 , 1 , 5 , 21 , 9 , 2 , 6 , 22 , 86 , 13 ,
does> swap cells + @ ;
does> swap cells + @ ;


Line 2,885: Line 2,885:
create (roman) 16 chars allot
create (roman) 16 chars allot


1999 (roman) >roman type cr</lang>
1999 (roman) >roman type cr</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran>program roman_numerals
<syntaxhighlight lang="fortran">program roman_numerals


implicit none
implicit none
Line 2,923: Line 2,923:
end function roman
end function roman


end program roman_numerals</lang>
end program roman_numerals</syntaxhighlight>
{{out}}
{{out}}
Line 2,933: Line 2,933:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function romanEncode(n As Integer) As String
Function romanEncode(n As Integer) As String
Line 2,963: Line 2,963:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 2,973: Line 2,973:


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>window 1
<syntaxhighlight lang="futurebasic">window 1


local fn DecimaltoRoman( decimal as short ) as Str15
local fn DecimaltoRoman( decimal as short ) as Str15
Line 3,008: Line 3,008:
print " 33 = "; fn DecimaltoRoman( 33 )
print " 33 = "; fn DecimaltoRoman( 33 )


HandleEvents</lang>
HandleEvents</syntaxhighlight>


Output:
Output:
Line 3,027: Line 3,027:


If you see boxes in the code below, those are supposed to be the Unicode combining overline (U+0305) and look like {{overline|IVXLCDM}}. Or, if you see overstruck combinations of letters, that's a different font rendering problem. (If you need roman numerals > 3999 reliably, it might best to stick to chiseling them in stone...)
If you see boxes in the code below, those are supposed to be the Unicode combining overline (U+0305) and look like {{overline|IVXLCDM}}. Or, if you see overstruck combinations of letters, that's a different font rendering problem. (If you need roman numerals > 3999 reliably, it might best to stick to chiseling them in stone...)
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 3,065: Line 3,065:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,074: Line 3,074:


=={{header|Golo}}==
=={{header|Golo}}==
<lang golo>#!/usr/bin/env golosh
<syntaxhighlight lang="golo">#!/usr/bin/env golosh
----
----
This module takes a decimal integer and converts it to a Roman numeral.
This module takes a decimal integer and converts it to a Roman numeral.
Line 3,132: Line 3,132:
println("2008 == MMVIII? " + (2008: encode() == "MMVIII"))
println("2008 == MMVIII? " + (2008: encode() == "MMVIII"))
println("1666 == MDCLXVI? " + (1666: encode() == "MDCLXVI"))
println("1666 == MDCLXVI? " + (1666: encode() == "MDCLXVI"))
}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>symbols = [ 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' ]
<syntaxhighlight lang="groovy">symbols = [ 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' ]


def roman(arabic) {
def roman(arabic) {
Line 3,162: Line 3,162:
assert roman(1666) == 'MDCLXVI'
assert roman(1666) == 'MDCLXVI'
assert roman(1990) == 'MCMXC'
assert roman(1990) == 'MCMXC'
assert roman(2008) == 'MMVIII'</lang>
assert roman(2008) == 'MMVIII'</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 3,168: Line 3,168:
With an explicit decimal digit representation list:
With an explicit decimal digit representation list:


<lang haskell>digit :: Char -> Char -> Char -> Integer -> String
<syntaxhighlight lang="haskell">digit :: Char -> Char -> Char -> Integer -> String
digit x y z k =
digit x y z k =
[[x], [x, x], [x, x, x], [x, y], [y], [y, x], [y, x, x], [y, x, x, x], [x, z]] !!
[[x], [x, x], [x, x, x], [x, y], [y], [y, x], [y, x, x], [y, x, x, x], [x, z]] !!
Line 3,190: Line 3,190:


main :: IO ()
main :: IO ()
main = print $ toRoman <$> [1999, 25, 944]</lang>
main = print $ toRoman <$> [1999, 25, 944]</syntaxhighlight>
{{out}}
{{out}}
<pre>["MCMXCIX","XXV","CMXLIV"]</pre>
<pre>["MCMXCIX","XXV","CMXLIV"]</pre>
Line 3,196: Line 3,196:
or, defining '''romanFromInt''' in terms of mapAccumL
or, defining '''romanFromInt''' in terms of mapAccumL


<lang haskell>import Data.Bifunctor (first)
<syntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List (mapAccumL)
import Data.List (mapAccumL)
import Data.Tuple (swap)
import Data.Tuple (swap)
Line 3,213: Line 3,213:


main :: IO ()
main :: IO ()
main = (putStrLn . unlines) (roman <$> [1666, 1990, 2008, 2016, 2018])</lang>
main = (putStrLn . unlines) (roman <$> [1666, 1990, 2008, 2016, 2018])</syntaxhighlight>
{{Out}}
{{Out}}
<pre>MDCLXVI
<pre>MDCLXVI
Line 3,223: Line 3,223:
With the Roman patterns abstracted, and in a simple logic programming idiom:
With the Roman patterns abstracted, and in a simple logic programming idiom:


<lang haskell>
<syntaxhighlight lang="haskell">
module Main where
module Main where


Line 3,283: Line 3,283:
(if roman == expected then "PASS"
(if roman == expected then "PASS"
else ("FAIL, expected " ++ (show expected))) ++ ")"
else ("FAIL, expected " ++ (show expected))) ++ ")"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,292: Line 3,292:


=={{header|HicEst}}==
=={{header|HicEst}}==
<lang hicest>CHARACTER Roman*20
<syntaxhighlight lang="hicest">CHARACTER Roman*20


CALL RomanNumeral(1990, Roman) ! MCMXC
CALL RomanNumeral(1990, Roman) ! MCMXC
Line 3,313: Line 3,313:
ENDDO
ENDDO
ENDDO
ENDDO
END</lang>
END</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link numbers # commas, roman
<syntaxhighlight lang="icon">link numbers # commas, roman


procedure main(arglist)
procedure main(arglist)
every x := !arglist do
every x := !arglist do
write(commas(x), " -> ",roman(x)|"*** can't convert to Roman numerals ***")
write(commas(x), " -> ",roman(x)|"*** can't convert to Roman numerals ***")
end</lang>
end</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides roman] as seen below and is based upon a James Gimple SNOBOL4 function.
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides roman] as seen below and is based upon a James Gimple SNOBOL4 function.


<lang Icon>procedure roman(n) #: convert integer to Roman numeral
<syntaxhighlight lang="icon">procedure roman(n) #: convert integer to Roman numeral
local arabic, result
local arabic, result
static equiv
static equiv
Line 3,337: Line 3,337:
result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1]
result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1]
if find("*",result) then fail else return result
if find("*",result) then fail else return result
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,354: Line 3,354:
INTERCAL outputs numbers as Roman numerals by default, so this is surprisingly trivial for a language that generally tries to make things as difficult as possible. Although you do still have to <i>input</i> the numbers as spelled out digitwise in all caps.
INTERCAL outputs numbers as Roman numerals by default, so this is surprisingly trivial for a language that generally tries to make things as difficult as possible. Although you do still have to <i>input</i> the numbers as spelled out digitwise in all caps.


<lang intercal> PLEASE WRITE IN .1
<syntaxhighlight lang="intercal"> PLEASE WRITE IN .1
DO READ OUT .1
DO READ OUT .1
DO GIVE UP</lang>
DO GIVE UP</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,369: Line 3,369:


{{trans|C#}}
{{trans|C#}}
<lang Io>Roman := Object clone do (
<syntaxhighlight lang="io">Roman := Object clone do (
nums := list(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums := list(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
rum := list("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
rum := list("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
Line 3,386: Line 3,386:
)
)


Roman numeral(1666) println</lang>
Roman numeral(1666) println</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
<tt>rfd</tt> obtains Roman numerals from decimals.
<tt>rfd</tt> obtains Roman numerals from decimals.


<lang j>R1000=. ;L:1 ,{ <@(<;._1);._2]0 :0
<syntaxhighlight lang="j">R1000=. ;L:1 ,{ <@(<;._1);._2]0 :0
C CC CCC CD D DC DCC DCCC CM
C CC CCC CD D DC DCC DCCC CM
X XX XXX XL L LX LXX LXXX XC
X XX XXX XL L LX LXX LXXX XC
Line 3,397: Line 3,397:
)
)


rfd=: ('M' $~ <.@%&1000) , R1000 {::~ 1000&|</lang>
rfd=: ('M' $~ <.@%&1000) , R1000 {::~ 1000&|</syntaxhighlight>


Explanation: R1000's definition contains rows representing each of 10 different digits in the 100s, 10s and 1s column (the first entry in each row is blank -- each entry is preceded by a space). R1000 itself represents the first 1000 roman numerals (the cartesian product of these three rows of roman numeral "digits" which is constructed so that they are in numeric order. And the first entry -- zero -- is just blank). To convert a number to its roman numeral representation, we will separate the number into the integer part after dividing by 1000 (that's the number of 'M's we need) and the remainder after dividing by 1000 (which will be an index into R1000).
Explanation: R1000's definition contains rows representing each of 10 different digits in the 100s, 10s and 1s column (the first entry in each row is blank -- each entry is preceded by a space). R1000 itself represents the first 1000 roman numerals (the cartesian product of these three rows of roman numeral "digits" which is constructed so that they are in numeric order. And the first entry -- zero -- is just blank). To convert a number to its roman numeral representation, we will separate the number into the integer part after dividing by 1000 (that's the number of 'M's we need) and the remainder after dividing by 1000 (which will be an index into R1000).


For example:<lang j> rfd 1234
For example:<syntaxhighlight lang="j"> rfd 1234
MCCXXXIV
MCCXXXIV
rfd 567
rfd 567
DLXVII
DLXVII
rfd 89
rfd 89
LXXXIX</lang>
LXXXIX</syntaxhighlight>


Derived from the [[j:Essays/Roman Numerals|J Wiki]]. Further examples of use will be found there.
Derived from the [[j:Essays/Roman Numerals|J Wiki]]. Further examples of use will be found there.
Line 3,415: Line 3,415:
The conversion function throws an IllegalArgumentException for non-positive numbers, since Java does not have unsigned primitives.
The conversion function throws an IllegalArgumentException for non-positive numbers, since Java does not have unsigned primitives.
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<lang java5>public class RN {
<syntaxhighlight lang="java5">public class RN {


enum Numeral {
enum Numeral {
Line 3,455: Line 3,455:
}
}


}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1999 = MCMXCIX
<pre>1999 = MCMXCIX
Line 3,465: Line 3,465:
at RN.main(RN.java:38)</pre>
at RN.main(RN.java:38)</pre>
{{works with|Java|1.8+}}
{{works with|Java|1.8+}}
<lang java5>import java.util.Set;
<syntaxhighlight lang="java5">import java.util.Set;
import java.util.EnumSet;
import java.util.EnumSet;
import java.util.Collections;
import java.util.Collections;
Line 3,523: Line 3,523:
LongStream.of(1999, 25, 944).forEach(RomanNumerals::test);
LongStream.of(1999, 25, 944).forEach(RomanNumerals::test);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1999 = MCMXCIX
<pre>1999 = MCMXCIX
Line 3,538: Line 3,538:


{{trans|Tcl}}
{{trans|Tcl}}
<lang javascript>var roman = {
<syntaxhighlight lang="javascript">var roman = {
map: [
map: [
1000, 'M', 900, 'CM', 500, 'D', 400, 'CD', 100, 'C', 90, 'XC',
1000, 'M', 900, 'CM', 500, 'D', 400, 'CD', 100, 'C', 90, 'XC',
Line 3,555: Line 3,555:
}
}


roman.int_to_roman(1999); // "MCMXCIX"</lang>
roman.int_to_roman(1999); // "MCMXCIX"</syntaxhighlight>


====Functional composition====
====Functional composition====


<lang JavaScript>(function () {
<syntaxhighlight lang="javascript">(function () {
'use strict';
'use strict';


Line 3,607: Line 3,607:
romanTranscription);
romanTranscription);


})();</lang>
})();</syntaxhighlight>


{{Out}}
{{Out}}
<lang JavaScript>["MMXVI", "MCMXC", "MMVIII", "XIV.IX.MMXV", "MM", "MDCLXVI"]</lang>
<syntaxhighlight lang="javascript">["MMXVI", "MCMXC", "MMVIII", "XIV.IX.MMXV", "MM", "MDCLXVI"]</syntaxhighlight>


===ES6===
===ES6===
Line 3,616: Line 3,616:
{{Trans|Haskell}}
{{Trans|Haskell}}
(mapAccumL version)
(mapAccumL version)
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 3,693: Line 3,693:
// MAIN --
// MAIN --
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>MDCLXVI
<pre>MDCLXVI
Line 3,703: Line 3,703:


====Declarative====
====Declarative====
<lang JavaScript>function toRoman(num) {
<syntaxhighlight lang="javascript">function toRoman(num) {
return 'I'
return 'I'
.repeat(num)
.repeat(num)
Line 3,720: Line 3,720:
}
}


console.log(toRoman(1666));</lang>
console.log(toRoman(1666));</syntaxhighlight>
{{Out}}
{{Out}}
<lang JavaScript>MDCLXVI</lang>
<syntaxhighlight lang="javascript">MDCLXVI</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 3,738: Line 3,738:


===Easy-to-code version===
===Easy-to-code version===
<lang jq>def to_roman_numeral:
<syntaxhighlight lang="jq">def to_roman_numeral:
def romans:
def romans:
[100000, "\u2188"],
[100000, "\u2188"],
Line 3,769: Line 3,769:
| .n = .n - $i ) )
| .n = .n - $i ) )
| .res
| .res
end ;</lang>
end ;</syntaxhighlight>
'''Test Cases'''
'''Test Cases'''
<lang jq>def testcases: [1668, 1990, 2008, 2020, 4444, 5000, 8999, 39999, 89999, 399999];
<syntaxhighlight lang="jq">def testcases: [1668, 1990, 2008, 2020, 4444, 5000, 8999, 39999, 89999, 399999];


"Decimal => Roman:",
"Decimal => Roman:",
(testcases[]
(testcases[]
| " \(.) => \(to_roman_numeral)" )</lang>
| " \(.) => \(to_roman_numeral)" )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,793: Line 3,793:
==="Orders of Magnitude" version===
==="Orders of Magnitude" version===
'''Translated from [[#Julia|Julia]]''' extended to 399,999
'''Translated from [[#Julia|Julia]]''' extended to 399,999
<lang jq>def digits: tostring | explode | map( [.]|implode|tonumber);
<syntaxhighlight lang="jq">def digits: tostring | explode | map( [.]|implode|tonumber);
# Non-negative integer to Roman numeral up to 399,999
# Non-negative integer to Roman numeral up to 399,999
def to_roman_numeral:
def to_roman_numeral:
Line 3,812: Line 3,812:
| .rnum
| .rnum
end;
end;
</syntaxhighlight>
</lang>


=={{header|Jsish}}==
=={{header|Jsish}}==
This covers both Encode (toRoman) and Decode (fromRoman).
This covers both Encode (toRoman) and Decode (fromRoman).


<lang javascript>/* Roman numerals, in Jsish */
<syntaxhighlight lang="javascript">/* Roman numerals, in Jsish */
var Roman = {
var Roman = {
ord: ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'],
ord: ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'],
Line 3,872: Line 3,872:
Roman.toRoman(1666) ==> MDCLXVI
Roman.toRoman(1666) ==> MDCLXVI
=!EXPECTEND!=
=!EXPECTEND!=
*/</lang>
*/</syntaxhighlight>


{{out}}
{{out}}
Line 3,879: Line 3,879:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Printf
<syntaxhighlight lang="julia">using Printf


function romanencode(n::Integer)
function romanencode(n::Integer)
Line 3,912: Line 3,912:
for n in testcases
for n in testcases
@printf("%-4i => %s\n", n, romanencode(n))
@printf("%-4i => %s\n", n, romanencode(n))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,933: Line 3,933:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>val romanNumerals = mapOf(
<syntaxhighlight lang="scala">val romanNumerals = mapOf(
1000 to "M",
1000 to "M",
900 to "CM",
900 to "CM",
Line 3,968: Line 3,968:
println(encode(1666))
println(encode(1666))
println(encode(2008))
println(encode(2008))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,977: Line 3,977:
</pre>
</pre>
Alternatively:
Alternatively:
<lang scala>fun Int.toRomanNumeral(): String {
<syntaxhighlight lang="scala">fun Int.toRomanNumeral(): String {
fun digit(k: Int, unit: String, five: String, ten: String): String {
fun digit(k: Int, unit: String, five: String, ten: String): String {
return when (k) {
return when (k) {
Line 3,995: Line 3,995:
else -> throw IllegalArgumentException("${this} not in range 0..3999")
else -> throw IllegalArgumentException("${this} not in range 0..3999")
}
}
}</lang>
}</syntaxhighlight>


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>define br => '\r'
<syntaxhighlight lang="lasso">define br => '\r'
// encode roman
// encode roman
define encodeRoman(num::integer)::string => {
define encodeRoman(num::integer)::string => {
Line 4,016: Line 4,016:
'2008 in roman is '+encodeRoman(2008)
'2008 in roman is '+encodeRoman(2008)
br
br
'1666 in roman is '+encodeRoman(1666)</lang>
'1666 in roman is '+encodeRoman(1666)</syntaxhighlight>


=={{header|LaTeX}}==
=={{header|LaTeX}}==
The macro <code>\Roman</code> is defined for uppercase roman numeral, accepting as ''argument'' a name of an existing counter.
The macro <code>\Roman</code> is defined for uppercase roman numeral, accepting as ''argument'' a name of an existing counter.


<lang latex>\documentclass{minimal}
<syntaxhighlight lang="latex">\documentclass{minimal}
\newcounter{currentyear}
\newcounter{currentyear}
\setcounter{currentyear}{\year}
\setcounter{currentyear}{\year}
\begin{document}
\begin{document}
Anno Domini \Roman{currentyear}
Anno Domini \Roman{currentyear}
\end{document}</lang>
\end{document}</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim arabic( 12)
dim arabic( 12)
for i =0 to 12
for i =0 to 12
Line 4,061: Line 4,061:
toRoman$ =result$
toRoman$ =result$
end function
end function
</syntaxhighlight>
</lang>
<pre>
<pre>
2009 MMIX
2009 MMIX
Line 4,069: Line 4,069:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>function toRoman intNum
<syntaxhighlight lang="livecode">function toRoman intNum
local roman,numArabic
local roman,numArabic
put "M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I" into romans
put "M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I" into romans
Line 4,090: Line 4,090:
end repeat
end repeat
return cc
return cc
end repeatChar</lang>
end repeatChar</syntaxhighlight>


Examples
Examples
Line 4,099: Line 4,099:


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>make "roman.rules [
<syntaxhighlight lang="logo">make "roman.rules [
[1000 M] [900 CM] [500 D] [400 CD]
[1000 M] [900 CM] [500 D] [400 CD]
[ 100 C] [ 90 XC] [ 50 L] [ 40 XL]
[ 100 C] [ 90 XC] [ 50 L] [ 40 XL]
Line 4,110: Line 4,110:
if :n < first first :rules [output (roman :n bf :rules :acc)]
if :n < first first :rules [output (roman :n bf :rules :acc)]
output (roman :n - first first :rules :rules word :acc last first :rules)
output (roman :n - first first :rules :rules word :acc last first :rules)
end</lang>
end</syntaxhighlight>


{{works with|UCB Logo}}
{{works with|UCB Logo}}
<lang logo>make "patterns [[?] [? ?] [? ? ?] [? ?2] [?2] [?2 ?] [?2 ? ?] [?2 ? ? ?] [? ?3]]
<syntaxhighlight lang="logo">make "patterns [[?] [? ?] [? ? ?] [? ?2] [?2] [?2 ?] [?2 ? ?] [?2 ? ? ?] [? ?3]]


to digit :d :numerals
to digit :d :numerals
Line 4,130: Line 4,130:
print roman 1999 ; MCMXCIX
print roman 1999 ; MCMXCIX
print roman 25 ; XXV
print roman 25 ; XXV
print roman 944 ; CMXLIV</lang>
print roman 944 ; CMXLIV</syntaxhighlight>


=={{header|LOLCODE}}==
=={{header|LOLCODE}}==
<lang lolcode>HAI 1.2
<syntaxhighlight lang="lolcode">HAI 1.2
I HAS A Romunz ITZ A BUKKIT
I HAS A Romunz ITZ A BUKKIT
Romunz HAS A SRS 0 ITZ "M"
Romunz HAS A SRS 0 ITZ "M"
Line 4,182: Line 4,182:
VISIBLE SMOOSH 1666 " = " I IZ Romunize YR 1666 MKAY MKAY
VISIBLE SMOOSH 1666 " = " I IZ Romunize YR 1666 MKAY MKAY
VISIBLE SMOOSH 3888 " = " I IZ Romunize YR 3888 MKAY MKAY
VISIBLE SMOOSH 3888 " = " I IZ Romunize YR 3888 MKAY MKAY
KTHXBYE</lang>
KTHXBYE</syntaxhighlight>


{{Out}}
{{Out}}
Line 4,190: Line 4,190:


=={{header|LotusScript}}==
=={{header|LotusScript}}==
<lang lss>
<syntaxhighlight lang="lss">
Function toRoman(value) As String
Function toRoman(value) As String
Dim arabic(12) As Integer
Dim arabic(12) As Integer
Line 4,235: Line 4,235:
End Function
End Function


</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==


<lang lua>romans = {
<syntaxhighlight lang="lua">romans = {
{1000, "M"},
{1000, "M"},
{900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"},
{900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"},
Line 4,253: Line 4,253:
end
end
end
end
print()</lang>
print()</syntaxhighlight>


=={{header|M4}}==
=={{header|M4}}==
<lang M4>define(`roman',`ifelse(eval($1>=1000),1,`M`'roman(eval($1-1000))',
<syntaxhighlight lang="m4">define(`roman',`ifelse(eval($1>=1000),1,`M`'roman(eval($1-1000))',
`ifelse(eval($1>=900),1,`CM`'roman(eval($1-900))',
`ifelse(eval($1>=900),1,`CM`'roman(eval($1-900))',
`ifelse(eval($1>=500),1,`D`'roman(eval($1-500))',
`ifelse(eval($1>=500),1,`D`'roman(eval($1-500))',
Line 4,270: Line 4,270:
)')')')')')')')')')')')')dnl
)')')')')')')')')')')')')dnl
dnl
dnl
roman(3675)</lang>
roman(3675)</syntaxhighlight>


{{out}}
{{out}}
Line 4,278: Line 4,278:


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>> for n in [ 1666, 1990, 2008 ] do printf( "%d\t%s\n", n, convert( n, 'roman' ) ) end:
<syntaxhighlight lang="maple">> for n in [ 1666, 1990, 2008 ] do printf( "%d\t%s\n", n, convert( n, 'roman' ) ) end:
1666 MDCLXVI
1666 MDCLXVI
1990 MCMXC
1990 MCMXC
2008 MMVIII</lang>
2008 MMVIII</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
RomanNumeral is a built-in function in the Wolfram language. Examples:
RomanNumeral is a built-in function in the Wolfram language. Examples:
<lang Mathematica>RomanNumeral[4]
<syntaxhighlight lang="mathematica">RomanNumeral[4]
RomanNumeral[99]
RomanNumeral[99]
RomanNumeral[1337]
RomanNumeral[1337]
RomanNumeral[1666]
RomanNumeral[1666]
RomanNumeral[6889]</lang>
RomanNumeral[6889]</syntaxhighlight>
gives back:
gives back:
<pre>IV
<pre>IV
Line 4,321: Line 4,321:
=== roman.m ===
=== roman.m ===


<syntaxhighlight lang="mercury">
<lang Mercury>
:- module roman.
:- module roman.


Line 4,374: Line 4,374:


:- end_module roman.
:- end_module roman.
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,400: Line 4,400:
Another implementation using an algorithm inspired by [[#Erlang|the Erlang implementation]] could look like this:
Another implementation using an algorithm inspired by [[#Erlang|the Erlang implementation]] could look like this:


<syntaxhighlight lang="mercury">
<lang Mercury>
:- module roman2.
:- module roman2.


Line 4,449: Line 4,449:


:- end_module roman2.
:- end_module roman2.
</syntaxhighlight>
</lang>


This implementation calculates the value of the thousands, then the hundreds, then the tens, then the ones. In each case it uses the <code>digit/4</code> function and some tricks with unification to build the appropriate list of characters for the digit and multiplier being targeted.
This implementation calculates the value of the thousands, then the hundreds, then the tens, then the ones. In each case it uses the <code>digit/4</code> function and some tricks with unification to build the appropriate list of characters for the digit and multiplier being targeted.
Line 4,457: Line 4,457:
=={{header|Microsoft Small Basic}}==
=={{header|Microsoft Small Basic}}==
{{trans|DWScript}}
{{trans|DWScript}}
<lang microsoftsmallbasic>
<syntaxhighlight lang="microsoftsmallbasic">
arabicNumeral = 1990
arabicNumeral = 1990
ConvertToRoman()
ConvertToRoman()
Line 4,505: Line 4,505:
EndWhile
EndWhile
EndSub
EndSub
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,516: Line 4,516:
{{trans|DWScript}}
{{trans|DWScript}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<lang modula2>
<syntaxhighlight lang="modula2">
MODULE RomanNumeralsEncode;
MODULE RomanNumeralsEncode;


Line 4,563: Line 4,563:
ToRoman(3888, Numeral); WriteString(Numeral); WriteLn; (* MMMDCCCLXXXVIII *)
ToRoman(3888, Numeral); WriteString(Numeral); WriteLn; (* MMMDCCCLXXXVIII *)
END RomanNumeralsEncode.
END RomanNumeralsEncode.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,572: Line 4,572:


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>TOROMAN(INPUT)
<syntaxhighlight lang="mumps">TOROMAN(INPUT)
;Converts INPUT into a Roman numeral. INPUT must be an integer between 1 and 3999
;Converts INPUT into a Roman numeral. INPUT must be an integer between 1 and 3999
;OUTPUT is the string to return
;OUTPUT is the string to return
Line 4,585: Line 4,585:
.FOR Q:CURRVAL<$PIECE(ROMANVAL,"^",I) SET OUTPUT=OUTPUT_$PIECE(ROMANNUM,"^",I),CURRVAL=CURRVAL-$PIECE(ROMANVAL,"^",I)
.FOR Q:CURRVAL<$PIECE(ROMANVAL,"^",I) SET OUTPUT=OUTPUT_$PIECE(ROMANNUM,"^",I),CURRVAL=CURRVAL-$PIECE(ROMANVAL,"^",I)
KILL I,CURRVAL
KILL I,CURRVAL
QUIT OUTPUT</lang>
QUIT OUTPUT</syntaxhighlight>
{{out}}
{{out}}
<pre>USER>W $$ROMAN^ROSETTA(1666)
<pre>USER>W $$ROMAN^ROSETTA(1666)
Line 4,599: Line 4,599:


Another variant
Another variant
<lang MUMPS>TOROMAN(n)
<syntaxhighlight lang="mumps">TOROMAN(n)
;return empty string if input parameter 'n' is not in 1-3999
;return empty string if input parameter 'n' is not in 1-3999
Quit:(n'?1.4N)!(n'<4000)!'n ""
Quit:(n'?1.4N)!(n'<4000)!'n ""
Line 4,609: Line 4,609:
. Set x=$Translate(x,"IVX",$Piece("IVX~XLC~CDM~M","~",p-j+1))
. Set x=$Translate(x,"IVX",$Piece("IVX~XLC~CDM~M","~",p-j+1))
. Set r=r_x
. Set r=r_x
Quit r</lang>
Quit r</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import strutils
<syntaxhighlight lang="nim">import strutils


const nums = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"),
const nums = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"),
Line 4,630: Line 4,630:
1000, 1009, 1444, 1666, 1945, 1997, 1999,
1000, 1009, 1444, 1666, 1945, 1997, 1999,
2000, 2008, 2010, 2011, 2500, 3000, 3999]:
2000, 2008, 2010, 2011, 2500, 3000, 3999]:
echo ($i).align(4), ": ", i.toRoman</lang>
echo ($i).align(4), ": ", i.toRoman</syntaxhighlight>


{{out}}
{{out}}
Line 4,690: Line 4,690:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|C sharp}}
{{trans|C sharp}}
<lang objeck>
<syntaxhighlight lang="objeck">
bundle Default {
bundle Default {
class Roman {
class Roman {
Line 4,723: Line 4,723:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 4,729: Line 4,729:
With an explicit decimal digit representation list:
With an explicit decimal digit representation list:


<lang ocaml>let digit x y z = function
<syntaxhighlight lang="ocaml">let digit x y z = function
1 -> [x]
1 -> [x]
| 2 -> [x;x]
| 2 -> [x;x]
Line 4,751: Line 4,751:
digit 'X' 'L' 'C' (x / 10) @ to_roman (x mod 10)
digit 'X' 'L' 'C' (x / 10) @ to_roman (x mod 10)
else
else
digit 'I' 'V' 'X' x</lang>
digit 'I' 'V' 'X' x</syntaxhighlight>


{{out}}
{{out}}
Line 4,765: Line 4,765:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>[ [1000,"M"], [900,"CM"], [500,"D"], [400,"CD"], [100,"C"], [90,"XC"], [50,"L"], [40,"XL"], [10,"X"], [9,"IX"], [5,"V"], [4,"IV"], [1,"I"] ] const: Romans
<syntaxhighlight lang="oforth">[ [1000,"M"], [900,"CM"], [500,"D"], [400,"CD"], [100,"C"], [90,"XC"], [50,"L"], [40,"XL"], [10,"X"], [9,"IX"], [5,"V"], [4,"IV"], [1,"I"] ] const: Romans


: roman(n)
: roman(n)
| r |
| r |
StringBuffer new
StringBuffer new
Romans forEach: r [ while(r first n <=) [ r second << n r first - ->n ] ] ;</lang>
Romans forEach: r [ while(r first n <=) [ r second << n r first - ->n ] ] ;</syntaxhighlight>


=={{header|OpenEdge/Progress}}==
=={{header|OpenEdge/Progress}}==
<lang progress>FUNCTION encodeRoman RETURNS CHAR (
<syntaxhighlight lang="progress">FUNCTION encodeRoman RETURNS CHAR (
i_i AS INT
i_i AS INT
):
):
Line 4,809: Line 4,809:
1666 encodeRoman( 1666 ) SKIP
1666 encodeRoman( 1666 ) SKIP
VIEW-AS ALERT-BOX.
VIEW-AS ALERT-BOX.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>---------------------------
<pre>---------------------------
Line 4,824: Line 4,824:
=={{header|Oz}}==
=={{header|Oz}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang oz>declare
<syntaxhighlight lang="oz">declare
fun {Digit X Y Z K}
fun {Digit X Y Z K}
unit([X] [X X] [X X X] [X Y] [Y] [Y X] [Y X X] [Y X X X] [X Z])
unit([X] [X X] [X X X] [X Y] [Y] [Y X] [Y X X] [Y X X X] [X Z])
Line 4,840: Line 4,840:
end
end
in
in
{ForAll {Map [1999 25 944] ToRoman} System.showInfo}</lang>
{ForAll {Map [1999 25 944] ToRoman} System.showInfo}</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Old-style Roman numerals
Old-style Roman numerals
<lang parigp>oldRoman(n)={
<syntaxhighlight lang="parigp">oldRoman(n)={
while(n>999999,
while(n>999999,
n-=1000000;
n-=1000000;
Line 4,898: Line 4,898:
);
);
print()
print()
};</lang>
};</syntaxhighlight>


This simple version of medieval Roman numerals does not handle large numbers.
This simple version of medieval Roman numerals does not handle large numbers.
<lang parigp>medievalRoman(n)={
<syntaxhighlight lang="parigp">medievalRoman(n)={
while(n>999,
while(n>999,
n-=1000;
n-=1000;
Line 4,955: Line 4,955:
);
);
print()
print()
};</lang>
};</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 4,962: Line 4,962:
=={{header|Peloton}}==
=={{header|Peloton}}==
Roman numbers are built in to Peloton as a particular form of national number. However, for the sake of the task the _RO opcode has been defined.
Roman numbers are built in to Peloton as a particular form of national number. However, for the sake of the task the _RO opcode has been defined.
<lang sgml><@ DEFUDOLITLIT>_RO|__Transformer|<@ DEFKEYPAR>__NationalNumericID|2</@><@ LETRESCS%NNMPAR>...|1</@></@>
<syntaxhighlight lang="sgml"><@ DEFUDOLITLIT>_RO|__Transformer|<@ DEFKEYPAR>__NationalNumericID|2</@><@ LETRESCS%NNMPAR>...|1</@></@>


<@ ENU$$DLSTLITLIT>1990,2008,1,2,64,124,1666,10001|,|
<@ ENU$$DLSTLITLIT>1990,2008,1,2,64,124,1666,10001|,|
<@ SAYELTLST>...</@> is <@ SAY_ROELTLSTLIT>...|RomanLowerUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanUpperUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanASCII</@>
<@ SAYELTLST>...</@> is <@ SAY_ROELTLSTLIT>...|RomanLowerUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanUpperUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanASCII</@>
</@></lang>
</@></syntaxhighlight>


Same code in padded-out, variable-length English dialect
Same code in padded-out, variable-length English dialect
<lang sgml><# DEFINE USERDEFINEDOPCODE LITERAL LITERAL>_RO|__Transformer|<# DEFINE KEYWORD PARAMETER>__NationalNumericID|2</#><# LET RESULT CAST NATIONALNUMBER PARAMETER>...|1</#></#>
<syntaxhighlight lang="sgml"><# DEFINE USERDEFINEDOPCODE LITERAL LITERAL>_RO|__Transformer|<# DEFINE KEYWORD PARAMETER>__NationalNumericID|2</#><# LET RESULT CAST NATIONALNUMBER PARAMETER>...|1</#></#>


<# ENUMERATION LAMBDASPECIFIEDDELMITER LIST LITERAL LITERAL>1990,2008,1,2,64,124,1666,10001|,|
<# ENUMERATION LAMBDASPECIFIEDDELMITER LIST LITERAL LITERAL>1990,2008,1,2,64,124,1666,10001|,|
<# SAY ELEMENT LIST>...</#> is <# SAY _RO ELEMENT LIST LITERAL>...|RomanLowerUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanUpperUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanASCII</#>
<# SAY ELEMENT LIST>...</#> is <# SAY _RO ELEMENT LIST LITERAL>...|RomanLowerUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanUpperUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanASCII</#>
</#></lang>
</#></syntaxhighlight>


{{out}} Notice here the three different ways of representing the results.
{{out}} Notice here the three different ways of representing the results.
Line 4,989: Line 4,989:
==== Simple program ====
==== Simple program ====
Simple, fast, produces same output as the Math::Roman module and the Raku example, less crazy than writing a Latin program, and doesn't require experimental modules like the Raku translation.
Simple, fast, produces same output as the Math::Roman module and the Raku example, less crazy than writing a Latin program, and doesn't require experimental modules like the Raku translation.
<lang perl>my @symbols = ( [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] );
<syntaxhighlight lang="perl">my @symbols = ( [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] );


sub roman {
sub roman {
Line 5,002: Line 5,002:
}
}


say roman($_) for 1..2012;</lang>
say roman($_) for 1..2012;</syntaxhighlight>


==== Using a module ====
==== Using a module ====
<lang perl>use Math::Roman qw/roman/;
<syntaxhighlight lang="perl">use Math::Roman qw/roman/;
say roman($_) for 1..2012'</lang>
say roman($_) for 1..2012'</syntaxhighlight>


==== Ported version of Raku ====
==== Ported version of Raku ====
<lang perl>use List::MoreUtils qw( natatime );
<syntaxhighlight lang="perl">use List::MoreUtils qw( natatime );


my %symbols = (
my %symbols = (
Line 5,031: Line 5,031:
};
};


print roman($_) . "\n" for 1..2012;</lang>
print roman($_) . "\n" for 1..2012;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">toRoman</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">toRoman</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">roman</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"M"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CM"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"D"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"L"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IX"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"V"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IV"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I"</span><span style="color: #0000FF;">},</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">roman</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"M"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CM"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"D"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"L"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IX"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"V"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IV"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I"</span><span style="color: #0000FF;">},</span>
Line 5,051: Line 5,051:
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1990</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2008</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1666</span><span style="color: #0000FF;">},</span><span style="color: #000000;">toRoman</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1990</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2008</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1666</span><span style="color: #0000FF;">},</span><span style="color: #000000;">toRoman</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 5,058: Line 5,058:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


def romanEnc /# n -- s #/
def romanEnc /# n -- s #/
Line 5,081: Line 5,081:
enddef
enddef


1968 romanEnc print</lang>
1968 romanEnc print</syntaxhighlight>
{{trans|Lua}}
{{trans|Lua}}
<lang Phixmonti>def romanEnc /# n -- s #/
<syntaxhighlight lang="phixmonti">def romanEnc /# n -- s #/
var k
var k
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" )
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" )
Line 5,100: Line 5,100:
enddef
enddef


1968 romanEnc</lang>
1968 romanEnc</syntaxhighlight>
Without vars
Without vars
<lang Phixmonti>def romanEnc /# n -- s #/
<syntaxhighlight lang="phixmonti">def romanEnc /# n -- s #/
>ps
>ps
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" )
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" )
Line 5,120: Line 5,120:
enddef
enddef


1968 romanEnc</lang>
1968 romanEnc</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
{{works with|PHP|4+ tested in 5.2.12}}
{{works with|PHP|4+ tested in 5.2.12}}
<lang php>
<syntaxhighlight lang="php">
/**
/**
* int2roman
* int2roman
Line 5,185: Line 5,185:
return $numeral . $leastSig;
return $numeral . $leastSig;
}
}
</syntaxhighlight>
</lang>


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
List = [455,999,1990,1999,2000,2001,2008,2009,2010,2011,2012,1666,3456,3888,4000],
List = [455,999,1990,1999,2000,2001,2008,2009,2010,2011,2012,1666,3456,3888,4000],
foreach(Val in List)
foreach(Val in List)
Line 5,208: Line 5,208:
end
end
end
end
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 5,229: Line 5,229:
===Longest numeral===
===Longest numeral===
Which number encodes to the longest Roman numerals in the interval 1..4000:
Which number encodes to the longest Roman numerals in the interval 1..4000:
<lang Picat>go2 =>
<syntaxhighlight lang="picat">go2 =>
All = [Len=I=roman_encode(I) : I in 1..4000,E=roman_encode(I), Len=E.len].sort_down,
All = [Len=I=roman_encode(I) : I in 1..4000,E=roman_encode(I), Len=E.len].sort_down,
println(All[1..2]),
println(All[1..2]),
nl.</lang>
nl.</syntaxhighlight>
{{out}}
{{out}}
Line 5,238: Line 5,238:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de roman (N)
<syntaxhighlight lang="picolisp">(de roman (N)
(pack
(pack
(make
(make
Line 5,247: Line 5,247:
(link C) ) )
(link C) ) )
'(M CM D CD C XC L XL X IX V IV I)
'(M CM D CD C XC L XL X IX V IV I)
(1000 900 500 400 100 90 50 40 10 9 5 4 1) ) ) ) )</lang>
(1000 900 500 400 100 90 50 40 10 9 5 4 1) ) ) ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>: (roman 1009)
<pre>: (roman 1009)
Line 5,256: Line 5,256:


=={{header|Pike}}==
=={{header|Pike}}==
<lang pike>import String;
<syntaxhighlight lang="pike">import String;
int main(){
int main(){
write(int2roman(2009) + "\n");
write(int2roman(2009) + "\n");
write(int2roman(1666) + "\n");
write(int2roman(1666) + "\n");
write(int2roman(1337) + "\n");
write(int2roman(1337) + "\n");
}</lang>
}</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* From Wiki Fortran */
/* From Wiki Fortran */
roman: procedure (n) returns(character (32) varying);
roman: procedure (n) returns(character (32) varying);
Line 5,284: Line 5,284:
return (r);
return (r);
end roman;
end roman;
</syntaxhighlight>
</lang>
Results:
Results:
<pre>
<pre>
Line 5,295: Line 5,295:


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
<syntaxhighlight lang="pl/sql">
<lang PL/SQL>


/*****************************************************************
/*****************************************************************
Line 5,321: Line 5,321:


END;
END;
</syntaxhighlight>
</lang>


=={{header|plainTeX}}==
=={{header|plainTeX}}==
TeX has its own way to convert a number into roman numeral, but it produces lowercase letters; the following macro (and usage example), produce uppercase roman numeral.
TeX has its own way to convert a number into roman numeral, but it produces lowercase letters; the following macro (and usage example), produce uppercase roman numeral.


<lang tex>\def\upperroman#1{\uppercase\expandafter{\romannumeral#1}}
<syntaxhighlight lang="tex">\def\upperroman#1{\uppercase\expandafter{\romannumeral#1}}
Anno Domini \upperroman{\year}
Anno Domini \upperroman{\year}
\bye</lang>
\bye</syntaxhighlight>


=={{header|PowerBASIC}}==
=={{header|PowerBASIC}}==
Line 5,337: Line 5,337:
{{works with|PB/CC|5}}
{{works with|PB/CC|5}}


<lang powerbasic>FUNCTION toRoman(value AS INTEGER) AS STRING
<syntaxhighlight lang="powerbasic">FUNCTION toRoman(value AS INTEGER) AS STRING
DIM arabic(0 TO 12) AS INTEGER
DIM arabic(0 TO 12) AS INTEGER
DIM roman(0 TO 12) AS STRING
DIM roman(0 TO 12) AS STRING
Line 5,360: Line 5,360:
? "1666 = " & toRoman(1666)
? "1666 = " & toRoman(1666)
? "3888 = " & toRoman(3888)
? "3888 = " & toRoman(3888)
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
Filter ToRoman {
Filter ToRoman {
$output = ''
$output = ''
Line 5,401: Line 5,401:
$output
$output
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
19,4,0,2479,3001 | ToRoman
19,4,0,2479,3001 | ToRoman
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 5,418: Line 5,418:
{{libheader|clpfd}}
{{libheader|clpfd}}
Library clpfd assures that the program works in both managements : Roman towards Arabic and Arabic towards Roman.
Library clpfd assures that the program works in both managements : Roman towards Arabic and Arabic towards Roman.
<lang Prolog>:- use_module(library(clpfd)).
<syntaxhighlight lang="prolog">:- use_module(library(clpfd)).


roman :-
roman :-
Line 5,520: Line 5,520:
my_print(A, R) :-
my_print(A, R) :-
format('~w in roman is ~w~n', [A, R]).
format('~w in roman is ~w~n', [A, R]).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> ?- roman.
<pre> ?- roman.
Line 5,532: Line 5,532:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#SymbolCount = 12 ;0 based count
<syntaxhighlight lang="purebasic">#SymbolCount = 12 ;0 based count
DataSection
DataSection
denominations:
denominations:
Line 5,587: Line 5,587:
Input()
Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
===Pythonic===
===Pythonic===
<lang python>import roman
<syntaxhighlight lang="python">import roman
print(roman.toRoman(2022))</lang>
print(roman.toRoman(2022))</syntaxhighlight>


===Imperative===
===Imperative===
# Version for Python 2
# Version for Python 2
<lang python>roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands #
<syntaxhighlight lang="python">roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands #
adjust_roman = "CCXXmmccxxii";
adjust_roman = "CCXXmmccxxii";
arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1);
arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1);
Line 5,619: Line 5,619:
2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000);
2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000);
for val in test:
for val in test:
print '%d - %s'%(val, arabic_to_roman(val))</lang>
print '%d - %s'%(val, arabic_to_roman(val))</syntaxhighlight>
An alternative which uses the divmod() function<lang python>romanDgts= 'ivxlcdmVXLCDM_'
An alternative which uses the divmod() function<syntaxhighlight lang="python">romanDgts= 'ivxlcdmVXLCDM_'


def ToRoman(num):
def ToRoman(num):
Line 5,635: Line 5,635:
else:
else:
namoR += r*romanDgts[rdix] + (romanDgts[rdix+1] if(v==1) else '')
namoR += r*romanDgts[rdix] + (romanDgts[rdix+1] if(v==1) else '')
return namoR[-1::-1]</lang>
return namoR[-1::-1]</syntaxhighlight>


It is more Pythonic to use zip to iterate over two lists together:
It is more Pythonic to use zip to iterate over two lists together:
<lang python>anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
<syntaxhighlight lang="python">anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
rnums = "M CM D CD C XC L XL X IX V IV I".split()
rnums = "M CM D CD C XC L XL X IX V IV I".split()


Line 5,656: Line 5,656:
for val in test:
for val in test:
print '%d - %s'%(val, to_roman(val))
print '%d - %s'%(val, to_roman(val))
</syntaxhighlight>
</lang>


# Version for Python 3
# Version for Python 3
<lang python>def arabic_to_roman(dclxvi):
<syntaxhighlight lang="python">def arabic_to_roman(dclxvi):
#===========================
#===========================
'''Convert an integer from the decimal notation to the Roman notation'''
'''Convert an integer from the decimal notation to the Roman notation'''
Line 5,680: Line 5,680:
for val in test:
for val in test:
print("%8d %s" %(val, arabic_to_roman(val)))</lang>
print("%8d %s" %(val, arabic_to_roman(val)))</syntaxhighlight>


===Declarative===
===Declarative===
Less readable, but a 'one liner':
Less readable, but a 'one liner':
<lang python>rnl = [ { '4' : 'MMMM', '3' : 'MMM', '2' : 'MM', '1' : 'M', '0' : '' }, { '9' : 'CM', '8' : 'DCCC', '7' : 'DCC',
<syntaxhighlight lang="python">rnl = [ { '4' : 'MMMM', '3' : 'MMM', '2' : 'MM', '1' : 'M', '0' : '' }, { '9' : 'CM', '8' : 'DCCC', '7' : 'DCC',
'6' : 'DC', '5' : 'D', '4' : 'CD', '3' : 'CCC', '2' : 'CC', '1' : 'C', '0' : '' }, { '9' : 'XC',
'6' : 'DC', '5' : 'D', '4' : 'CD', '3' : 'CCC', '2' : 'CC', '1' : 'C', '0' : '' }, { '9' : 'XC',
'8' : 'LXXX', '7' : 'LXX', '6' : 'LX', '5' : 'L', '4' : 'XL', '3' : 'XXX', '2' : 'XX', '1' : 'X',
'8' : 'LXXX', '7' : 'LXX', '6' : 'LX', '5' : 'L', '4' : 'XL', '3' : 'XXX', '2' : 'XX', '1' : 'X',
Line 5,694: Line 5,694:
# Option 2
# Option 2
def number2romannumeral(n):
def number2romannumeral(n):
return reduce(lambda x, y: x + y, map(lambda x, y: rnl[x][y], range(4), str(n).zfill(4))) if -1 < n < 5000 else None</lang>
return reduce(lambda x, y: x + y, map(lambda x, y: rnl[x][y], range(4), str(n).zfill(4))) if -1 < n < 5000 else None</syntaxhighlight>




Line 5,700: Line 5,700:
{{works with|Python|3}}
{{works with|Python|3}}
{{Trans|Haskell}}
{{Trans|Haskell}}
<lang python>'''Encoding Roman Numerals'''
<syntaxhighlight lang="python">'''Encoding Roman Numerals'''


from functools import reduce
from functools import reduce
Line 5,770: Line 5,770:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>MDCLXVI
<pre>MDCLXVI
Line 5,780: Line 5,780:


=={{header|QBasic}}==
=={{header|QBasic}}==
<lang QBasic>DIM SHARED arabic(0 TO 12)
<syntaxhighlight lang="qbasic">DIM SHARED arabic(0 TO 12)
DIM SHARED roman$(0 TO 12)
DIM SHARED roman$(0 TO 12)


Line 5,804: Line 5,804:
PRINT "2009 = "; toRoman$(2009)
PRINT "2009 = "; toRoman$(2009)
PRINT "1666 = "; toRoman$(1666)
PRINT "1666 = "; toRoman$(1666)
PRINT "3888 = "; toRoman$(3888)</lang>
PRINT "3888 = "; toRoman$(3888)</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 5,810: Line 5,810:
Pasting epitomised.
Pasting epitomised.


<lang Quackery> [ $ ""
<syntaxhighlight lang="quackery"> [ $ ""
swap 1000 /mod $ "M" rot of rot swap join swap
swap 1000 /mod $ "M" rot of rot swap join swap
dup 900 < not if [ 900 - dip [ $ "CM" join ] ]
dup 900 < not if [ 900 - dip [ $ "CM" join ] ]
Line 5,828: Line 5,828:
1990 dup echo say " = " ->roman echo$ cr
1990 dup echo say " = " ->roman echo$ cr
2008 dup echo say " = " ->roman echo$ cr
2008 dup echo say " = " ->roman echo$ cr
1666 dup echo say " = " ->roman echo$ cr</lang>
1666 dup echo say " = " ->roman echo$ cr</syntaxhighlight>


{{Out}}
{{Out}}
Line 5,838: Line 5,838:
=={{header|R}}==
=={{header|R}}==
R has a built-in function, <code>[https://svn.r-project.org/R/trunk/src/library/utils/R/roman.R as.roman]</code>, for conversion to Roman numerals. The implementation details are found in <code>utils:::.numeric2roman</code> (see previous link), and <code>utils:::.roman2numeric</code>, for conversion back to Arabic decimals.
R has a built-in function, <code>[https://svn.r-project.org/R/trunk/src/library/utils/R/roman.R as.roman]</code>, for conversion to Roman numerals. The implementation details are found in <code>utils:::.numeric2roman</code> (see previous link), and <code>utils:::.roman2numeric</code>, for conversion back to Arabic decimals.
<lang R>as.roman(1666) # MDCLXVI</lang>
<syntaxhighlight lang="r">as.roman(1666) # MDCLXVI</syntaxhighlight>
Since the object <code>as.roman</code> creates is just an integer vector with a class, you can do arithmetic with Roman numerals:
Since the object <code>as.roman</code> creates is just an integer vector with a class, you can do arithmetic with Roman numerals:
<lang R>as.roman(1666) + 334 # MM</lang>
<syntaxhighlight lang="r">as.roman(1666) + 334 # MM</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
Straight recursion:
Straight recursion:
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (encode/roman number)
(define (encode/roman number)
(cond ((>= number 1000) (string-append "M" (encode/roman (- number 1000))))
(cond ((>= number 1000) (string-append "M" (encode/roman (- number 1000))))
Line 5,859: Line 5,859:
((>= number 4) (string-append "IV" (encode/roman (- number 4))))
((>= number 4) (string-append "IV" (encode/roman (- number 4))))
((>= number 1) (string-append "I" (encode/roman (- number 1))))
((>= number 1) (string-append "I" (encode/roman (- number 1))))
(else "")))</lang>
(else "")))</syntaxhighlight>


Using for/fold and quotient/remainder to remove repetition:
Using for/fold and quotient/remainder to remove repetition:
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (number->list n)
(define (number->list n)
(for/fold ([result null])
(for/fold ([result null])
Line 5,879: Line 5,879:
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500
3000 3999)])
3000 3999)])
(printf "~a ~a\n" n (encode/roman n)))</lang>
(printf "~a ~a\n" n (encode/roman n)))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)


<lang perl6>my %symbols =
<syntaxhighlight lang="raku" line>my %symbols =
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C",
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C",
500 => "D", 1_000 => "M";
500 => "D", 1_000 => "M";
Line 5,905: Line 5,905:
for 1 .. 2_010 -> $x {
for 1 .. 2_010 -> $x {
say roman($x);
say roman($x);
}</lang>
}</syntaxhighlight>


=={{header|Red}}==
=={{header|Red}}==
Straight iterative solution:
Straight iterative solution:
<syntaxhighlight lang="red">
<lang Red>
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I]
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I]


Line 5,919: Line 5,919:


foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
</syntaxhighlight>
</lang>
Straight recursive solution:
Straight recursive solution:
<syntaxhighlight lang="red">
<lang Red>
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I]
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I]


Line 5,933: Line 5,933:


foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
</syntaxhighlight>
</lang>
This solution builds, using metaprogramming, a `case` table, that relies on recursion to convert every digit.
This solution builds, using metaprogramming, a `case` table, that relies on recursion to convert every digit.


<syntaxhighlight lang="red">
<lang Red>
to-Roman: function [n [integer!]] reduce [
to-Roman: function [n [integer!]] reduce [
'case collect [
'case collect [
Line 5,946: Line 5,946:


foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
foreach number [40 33 1888 2016][print [number ":" to-Roman number]]
</syntaxhighlight>
</lang>


=={{header|Retro}}==
=={{header|Retro}}==
This is a port of the [[Forth]] code; but returns a string rather than displaying the roman numerals. It only handles numbers between 1 and 3999.
This is a port of the [[Forth]] code; but returns a string rather than displaying the roman numerals. It only handles numbers between 1 and 3999.


<syntaxhighlight lang="retro">
<lang Retro>
: vector ( ...n"- )
: vector ( ...n"- )
here [ &, times ] dip : .data ` swap ` + ` @ ` do ` ; ;
here [ &, times ] dip : .data ` swap ` + ` @ ` do ` ; ;
Line 5,976: Line 5,976:
dup 1 3999 within 0 =
dup 1 3999 within 0 =
[ "EX LIMITO!\n" ] [ "IVXLCDM" swap record here ] if ;
[ "EX LIMITO!\n" ] [ "IVXLCDM" swap record here ] if ;
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>roman: procedure
<syntaxhighlight lang="rexx">roman: procedure
arg number
arg number


Line 5,996: Line 5,996:
end
end
end
end
return result</lang>
return result</syntaxhighlight>
===version 2===
===version 2===
This version of a REXX program allows almost any non-negative decimal integer.
This version of a REXX program allows almost any non-negative decimal integer.
Line 6,014: Line 6,014:
The general REXX code is bulkier than most at it deals with &nbsp; ''any'' &nbsp; non-negative decimal number, &nbsp; and more
The general REXX code is bulkier than most at it deals with &nbsp; ''any'' &nbsp; non-negative decimal number, &nbsp; and more
<br>boilerplate code is in the general REXX code to handle the above versions.
<br>boilerplate code is in the general REXX code to handle the above versions.
<lang rexx>/*REXX program converts (Arabic) non─negative decimal integers (≥0) ───► Roman numerals.*/
<syntaxhighlight lang="rexx">/*REXX program converts (Arabic) non─negative decimal integers (≥0) ───► Roman numerals.*/
numeric digits 10000 /*decimal digs can be higher if wanted.*/
numeric digits 10000 /*decimal digs can be higher if wanted.*/
parse arg # /*obtain optional integers from the CL.*/
parse arg # /*obtain optional integers from the CL.*/
Line 6,066: Line 6,066:
if pos(_, #)\==0 then #=changestr(_, #, copies('M', i))
if pos(_, #)\==0 then #=changestr(_, #, copies('M', i))
end /*i*/
end /*i*/
return #</lang>
return #</syntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]]. <br><br>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]]. <br><br>
'''output''' &nbsp; when using the default (internal) input):
'''output''' &nbsp; when using the default (internal) input):
Line 6,164: Line 6,164:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
roman = ["M", "CM", "D", "CD", "C" ,"XC", "L", "XL" ,"X", "IX", "V", "IV", "I"]
roman = ["M", "CM", "D", "CD", "C" ,"XC", "L", "XL" ,"X", "IX", "V", "IV", "I"]
Line 6,181: Line 6,181:
next
next
return result
return result
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Roman numeral generation was used as an example for demonstrating [http://www.xpsd.org/cgi-bin/wiki?TestDrivenDevelopmentTutorialRomanNumerals Test Driven Development] in Ruby. The solution came to be:
Roman numeral generation was used as an example for demonstrating [http://www.xpsd.org/cgi-bin/wiki?TestDrivenDevelopmentTutorialRomanNumerals Test Driven Development] in Ruby. The solution came to be:
<lang ruby>Symbols = { 1=>'I', 5=>'V', 10=>'X', 50=>'L', 100=>'C', 500=>'D', 1000=>'M' }
<syntaxhighlight lang="ruby">Symbols = { 1=>'I', 5=>'V', 10=>'X', 50=>'L', 100=>'C', 500=>'D', 1000=>'M' }
Subtractors = [ [1000, 100], [500, 100], [100, 10], [50, 10], [10, 1], [5, 1], [1, 0] ]
Subtractors = [ [1000, 100], [500, 100], [100, 10], [50, 10], [10, 1], [5, 1], [1, 0] ]


Line 6,198: Line 6,198:
[1990, 2008, 1666].each do |i|
[1990, 2008, 1666].each do |i|
puts "%4d => %s" % [i, roman(i)]
puts "%4d => %s" % [i, roman(i)]
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 6,209: Line 6,209:
Another shorter version if we don't consider calculating the substractors:
Another shorter version if we don't consider calculating the substractors:


<lang ruby>
<syntaxhighlight lang="ruby">
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]


Line 6,216: Line 6,216:
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep }
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep }
end
end
</syntaxhighlight>
</lang>


Yet another way to solve it in terms of reduce
Yet another way to solve it in terms of reduce


<lang ruby>
<syntaxhighlight lang="ruby">
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]


Line 6,229: Line 6,229:
end
end
end
end
</syntaxhighlight>
</lang>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>[loop]
<syntaxhighlight lang="runbasic">[loop]
input "Input value:";val$
input "Input value:";val$
print roman$(val$)
print roman$(val$)
Line 6,251: Line 6,251:
wend
wend
next i
next i
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>struct RomanNumeral {
<syntaxhighlight lang="rust">struct RomanNumeral {
symbol: &'static str,
symbol: &'static str,
value: u32
value: u32
Line 6,292: Line 6,292:
println!("{:2$} = {}", n, to_roman(n), 4);
println!("{:2$} = {}", n, to_roman(n), 4);
}
}
}</lang>{{out}}
}</syntaxhighlight>{{out}}
<pre>
<pre>
2014 = MMXIV
2014 = MMXIV
Line 6,303: Line 6,303:
=={{header|Scala}}==
=={{header|Scala}}==
{{works with|Scala|2.8}}
{{works with|Scala|2.8}}
<lang scala>val romanDigits = Map(
<syntaxhighlight lang="scala">val romanDigits = Map(
1 -> "I", 5 -> "V",
1 -> "I", 5 -> "V",
10 -> "X", 50 -> "L",
10 -> "X", 50 -> "L",
Line 6,315: Line 6,315:
case Some(key) => romanDigits(key) + toRoman(n - key)
case Some(key) => romanDigits(key) + toRoman(n - key)
case None => ""
case None => ""
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>scala> List(1990, 2008, 1666) map toRoman
<pre>scala> List(1990, 2008, 1666) map toRoman
res55: List[String] = List(MCMXC, MMVIII, MDCLXVI)</pre>
res55: List[String] = List(MCMXC, MMVIII, MDCLXVI)</pre>
===Using foldLeft===
===Using foldLeft===
<lang Scala>def toRoman( v:Int ) : String = {
<syntaxhighlight lang="scala">def toRoman( v:Int ) : String = {
val romanNumerals = List(1000->"M",900->"CM",500->"D",400->"CD",100->"C",90->"XC",
val romanNumerals = List(1000->"M",900->"CM",500->"D",400->"CD",100->"C",90->"XC",
50->"L",40->"XL",10->"X",9->"IX",5->"V",4->"IV",1->"I")
50->"L",40->"XL",10->"X",9->"IX",5->"V",4->"IV",1->"I")
Line 6,333: Line 6,333:
test(1990)
test(1990)
test(2008)
test(2008)
test(1666)</lang>
test(1666)</syntaxhighlight>
===Different code-style===
===Different code-style===
<lang Scala>def toRoman(num: Int): String = {
<syntaxhighlight lang="scala">def toRoman(num: Int): String = {
case class RomanUnit(value: Int, token: String)
case class RomanUnit(value: Int, token: String)
val romanNumerals = List(
val romanNumerals = List(
Line 6,360: Line 6,360:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1990 => MCMXC
<pre>1990 => MCMXC
Line 6,369: Line 6,369:
This uses format directives supported in Chez Scheme since v6.9b; YMMV.
This uses format directives supported in Chez Scheme since v6.9b; YMMV.


<lang scheme>(define (to-roman n)
<syntaxhighlight lang="scheme">(define (to-roman n)
(format "~@r" n))</lang>
(format "~@r" n))</syntaxhighlight>


This is a general example using Chicken Scheme.
This is a general example using Chicken Scheme.
<lang scheme>(define roman-decimal
<syntaxhighlight lang="scheme">(define roman-decimal
'(("M" . 1000)
'(("M" . 1000)
("CM" . 900)
("CM" . 900)
Line 6,407: Line 6,407:
(printf "~a ~a\n" (car n) (to-roman (car n)))
(printf "~a ~a\n" (car n) (to-roman (car n)))
(loop (cdr n))))
(loop (cdr n))))
</syntaxhighlight>
</lang>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 6,415: Line 6,415:
which writes a roman numeral to a string.
which writes a roman numeral to a string.


<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "stdio.s7i";
include "stdio.s7i";
include "wrinum.s7i";
include "wrinum.s7i";
Line 6,426: Line 6,426:
writeln(str(ROMAN, number));
writeln(str(ROMAN, number));
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


Original source [http://seed7.sourceforge.net/algorith/puzzles.htm#roman_numerals].
Original source [http://seed7.sourceforge.net/algorith/puzzles.htm#roman_numerals].
Line 6,432: Line 6,432:
=={{header|SenseTalk}}==
=={{header|SenseTalk}}==


<lang sensetalk>function RomanNumeralsEncode number
<syntaxhighlight lang="sensetalk">function RomanNumeralsEncode number
put [
put [
(1, "I"),
(1, "I"),
Line 6,458: Line 6,458:
end repeat
end repeat
return numerals
return numerals
end RomanNumeralsEncode</lang>
end RomanNumeralsEncode</syntaxhighlight>


<lang sensetalk>repeat for each item in [
<syntaxhighlight lang="sensetalk">repeat for each item in [
1990,
1990,
2008,
2008,
Line 6,466: Line 6,466:
]
]
put RomanNumeralsEncode(it)
put RomanNumeralsEncode(it)
end repeat</lang>
end repeat</syntaxhighlight>


{{out}}
{{out}}
Line 6,476: Line 6,476:


=={{header|SETL}}==
=={{header|SETL}}==
<lang ada>examples := [2008, 1666, 1990];
<syntaxhighlight lang="ada">examples := [2008, 1666, 1990];


for example in examples loop
for example in examples loop
Line 6,492: Line 6,492:
end loop;
end loop;
return roman;
return roman;
end;</lang>
end;</syntaxhighlight>
{{out}}
{{out}}
<pre>MMVIII
<pre>MMVIII
Line 6,499: Line 6,499:


=={{header|Shen}}==
=={{header|Shen}}==
<lang shen>
<syntaxhighlight lang="shen">
(define encodeGlyphs
(define encodeGlyphs
ACC 0 _ -> ACC
ACC 0 _ -> ACC
Line 6,509: Line 6,509:
N -> (encodeGlyphs "" N ["M" 1000 "CM" 900 "D" 500 "CD" 400 "C" 100 "XC" 90 "L" 50 "XL" 40 "X" 10 "IX" 9 "V" 5 "IV" 4 "I" 1])
N -> (encodeGlyphs "" N ["M" 1000 "CM" 900 "D" 500 "CD" 400 "C" 100 "XC" 90 "L" 50 "XL" 40 "X" 10 "IX" 9 "V" 5 "IV" 4 "I" 1])
)
)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 6,524: Line 6,524:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|ActionScript}}
{{trans|ActionScript}}
<lang ruby>func arabic2roman(num, roman='') {
<syntaxhighlight lang="ruby">func arabic2roman(num, roman='') {
static lookup = [
static lookup = [
:M:1000, :CM:900, :D:500,
:M:1000, :CM:900, :D:500,
Line 6,542: Line 6,542:
say("1990 in roman is " + arabic2roman(1990));
say("1990 in roman is " + arabic2roman(1990));
say("2008 in roman is " + arabic2roman(2008));
say("2008 in roman is " + arabic2roman(2008));
say("1666 in roman is " + arabic2roman(1666));</lang>
say("1666 in roman is " + arabic2roman(1666));</syntaxhighlight>
{{out}}
{{out}}
<pre>1990 in roman is MCMXC
<pre>1990 in roman is MCMXC
Line 6,549: Line 6,549:


=={{header|Simula}}==
=={{header|Simula}}==
<lang simula>BEGIN
<syntaxhighlight lang="simula">BEGIN


TEXT PROCEDURE TOROMAN(N); INTEGER N;
TEXT PROCEDURE TOROMAN(N); INTEGER N;
Line 6,589: Line 6,589:


END PROGRAM;
END PROGRAM;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 6,601: Line 6,601:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
in ST/X, integers already know how to print themselves as roman number:
in ST/X, integers already know how to print themselves as roman number:
<lang smalltalk>2013 printRomanOn:Stdout naive:false</lang>
<syntaxhighlight lang="smalltalk">2013 printRomanOn:Stdout naive:false</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
MMXIII</pre>
MMXIII</pre>
the implementation is:
the implementation is:
<lang smalltalk>
<syntaxhighlight lang="smalltalk">
printRomanOn:aStream naive:naive
printRomanOn:aStream naive:naive
"print the receiver as roman number to the argument, aStream.
"print the receiver as roman number to the argument, aStream.
Line 6,660: Line 6,660:
] doWhile:[ repeatFlag and:[ restValue >= rValue] ].
] doWhile:[ repeatFlag and:[ restValue >= rValue] ].
].
].
</syntaxhighlight>
</lang>


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
Adapted from [http://burks.bton.ac.uk/burks/language/snobol/catspaw/tutorial/ch6.htm Catspaw SNOBOL Tutorial, Chapter 6]
Adapted from [http://burks.bton.ac.uk/burks/language/snobol/catspaw/tutorial/ch6.htm Catspaw SNOBOL Tutorial, Chapter 6]


<lang snobol4>
<syntaxhighlight lang="snobol4">
* ROMAN(N) - Convert integer N to Roman numeral form.
* ROMAN(N) - Convert integer N to Roman numeral form.
*
*
Line 6,696: Line 6,696:
OUTPUT = " 944 = " ROMAN(944)
OUTPUT = " 944 = " ROMAN(944)


END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,706: Line 6,706:
Here's a non-recursive version, and a Roman-to-Arabic converter to boot.
Here's a non-recursive version, and a Roman-to-Arabic converter to boot.


<lang SNOBOL4>* # Arabic to Roman
<syntaxhighlight lang="snobol4">* # Arabic to Roman
define('roman(n)s,ch,val,str') :(roman_end)
define('roman(n)s,ch,val,str') :(roman_end)
roman roman = ge(n,4000) n :s(return)
roman roman = ge(n,4000) n :s(return)
Line 6,734: Line 6,734:
astr = astr r '=' arabic(r) ' ' :(tloop)
astr = astr r '=' arabic(r) ' ' :(tloop)
out output = rstr; output = astr
out output = rstr; output = astr
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 6,741: Line 6,741:


=={{header|SPL}}==
=={{header|SPL}}==
<lang spl>a2r(a)=
<syntaxhighlight lang="spl">a2r(a)=
r = ""
r = ""
n = [["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"],[1000,900,500,400,100,90,50,40,10,9,5,4,1]]
n = [["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"],[1000,900,500,400,100,90,50,40,10,9,5,4,1]]
Line 6,756: Line 6,756:
> i, 1..#.size(t,1)
> i, 1..#.size(t,1)
#.output(t[i]," = ",a2r(t[i]))
#.output(t[i]," = ",a2r(t[i]))
<</lang>
<</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,765: Line 6,765:


=={{header|SQL}}==
=={{header|SQL}}==
<syntaxhighlight lang="sql">
<lang SQL>
--
--
-- This only works under Oracle and has the limitation of 1 to 3999
-- This only works under Oracle and has the limitation of 1 to 3999
Line 6,775: Line 6,775:
--------------- ---------------
--------------- ---------------
MDCLXVI mdclxvi
MDCLXVI mdclxvi
</syntaxhighlight>
</lang>


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>func ator(var n: Int) -> String {
<syntaxhighlight lang="swift">func ator(var n: Int) -> String {


var result = ""
var result = ""
Line 6,803: Line 6,803:
}
}
return result
return result
}</lang>
}</syntaxhighlight>
Sample call:
Sample call:
{{works with|Swift|1.x}}
{{works with|Swift|1.x}}
<lang swift>println(ator(1666)) // MDCLXVI</lang>
<syntaxhighlight lang="swift">println(ator(1666)) // MDCLXVI</syntaxhighlight>
{{works with|Swift|2.0}}
{{works with|Swift|2.0}}
<lang swift>print(ator(1666)) // MDCLXVI</lang>
<syntaxhighlight lang="swift">print(ator(1666)) // MDCLXVI</syntaxhighlight>
{{output}}
{{output}}
<pre>MDCLXVI </pre>
<pre>MDCLXVI </pre>


=={{header|Tailspin}}==
=={{header|Tailspin}}==
<lang tailspin>
<syntaxhighlight lang="tailspin">
def digits: [(M:1000"1"), (CM:900"1"), (D:500"1"), (CD:400"1"), (C:100"1"), (XC:90"1"), (L:50"1"), (XL:40"1"), (X:10"1"), (IX:9"1"), (V:5"1"), (IV:4"1"), (I:1"1")];
def digits: [(M:1000"1"), (CM:900"1"), (D:500"1"), (CD:400"1"), (C:100"1"), (XC:90"1"), (L:50"1"), (XL:40"1"), (X:10"1"), (IX:9"1"), (V:5"1"), (IV:4"1"), (I:1"1")];
templates encodeRoman
templates encodeRoman
Line 6,833: Line 6,833:
' -> !OUT::write
' -> !OUT::write
1666 -> encodeRoman -> !OUT::write
1666 -> encodeRoman -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 6,842: Line 6,842:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc to_roman {i} {
<syntaxhighlight lang="tcl">proc to_roman {i} {
set map {1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I}
set map {1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I}
foreach {value roman} $map {
foreach {value roman} $map {
Line 6,851: Line 6,851:
}
}
return $res
return $res
}</lang>
}</syntaxhighlight>


=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
<lang ti83b>PROGRAM:DEC2ROM
<syntaxhighlight lang="ti83b">PROGRAM:DEC2ROM
:"="→Str1
:"="→Str1
:Lbl ST
:Lbl ST
Line 6,938: Line 6,938:
:Pause
:Pause
:Goto ST
:Goto ST
</syntaxhighlight>
</lang>


=={{header|True BASIC}}==
=={{header|True BASIC}}==
<lang qbasic>OPTION BASE 0
<syntaxhighlight lang="qbasic">OPTION BASE 0
DIM arabic(12), roman$(12)
DIM arabic(12), roman$(12)


Line 6,966: Line 6,966:
PRINT "1666 = "; toRoman$(1666)
PRINT "1666 = "; toRoman$(1666)
PRINT "3888 = "; toRoman$(3888)
PRINT "3888 = "; toRoman$(3888)
END</lang>
END</syntaxhighlight>




=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
LOOP arab_number="1990'2008'1666"
LOOP arab_number="1990'2008'1666"
Line 6,976: Line 6,976:
PRINT "Arabic number ",arab_number, " equals ", roman_number
PRINT "Arabic number ",arab_number, " equals ", roman_number
ENDLOOP
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 6,987: Line 6,987:
{{trans|DWScript}}
{{trans|DWScript}}
Weights and symbols in tuples.
Weights and symbols in tuples.
<lang javascript>
<syntaxhighlight lang="javascript">
// Roman numerals/Encode
// Roman numerals/Encode


Line 7,011: Line 7,011:
console.log(toRoman(2022)); // MMXXII
console.log(toRoman(2022)); // MMXXII
console.log(toRoman(3888)); // MMMDCCCLXXXVIII
console.log(toRoman(3888)); // MMMDCCCLXXXVIII
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 7,021: Line 7,021:
=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
{{trans|BBC Basic}}
{{trans|BBC Basic}}
<lang>Push 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
<syntaxhighlight lang="text">Push 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
' Initialize array
' Initialize array
For i = 12 To 0 Step -1
For i = 12 To 0 Step -1
Line 7,059: Line 7,059:
110 Print "D"; : Return
110 Print "D"; : Return
120 Print "CM"; : Return
120 Print "CM"; : Return
130 Print "M"; : Return</lang>
130 Print "M"; : Return</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{trans|Tcl}}
{{trans|Tcl}}
{{works with|bash}}
{{works with|bash}}
<lang bash>roman() {
<syntaxhighlight lang="bash">roman() {
local values=( 1000 900 500 400 100 90 50 40 10 9 5 4 1 )
local values=( 1000 900 500 400 100 90 50 40 10 9 5 4 1 )
local roman=(
local roman=(
Line 7,085: Line 7,085:
for test in 1999 24 944 1666 2008; do
for test in 1999 24 944 1666 2008; do
printf "%d = %s\n" $test $(roman $test)
printf "%d = %s\n" $test $(roman $test)
done</lang>
done</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 7,103: Line 7,103:
CCCC are replaced by CD. The substitution operator (%=) is helpful
CCCC are replaced by CD. The substitution operator (%=) is helpful
here.
here.
<lang Ursala>#import nat
<syntaxhighlight lang="ursala">#import nat


roman =
roman =
Line 7,109: Line 7,109:
-+
-+
'IIII'%='IV'+ 'VIIII'%='IX'+ 'XXXX'%='XL'+ 'LXXXX'%='XC'+ 'CCCC'%='CD'+ 'DCCCC'%='CM',
'IIII'%='IV'+ 'VIIII'%='IX'+ 'XXXX'%='XL'+ 'LXXXX'%='XC'+ 'CCCC'%='CD'+ 'DCCCC'%='CM',
~&plrDlSPSL/'MDCLXVI'+ iota*+ +^|(^|C/~&,\/division)@rlX=>~&iNC <1000,500,100,50,10,5>+-</lang>
~&plrDlSPSL/'MDCLXVI'+ iota*+ +^|(^|C/~&,\/division)@rlX=>~&iNC <1000,500,100,50,10,5>+-</syntaxhighlight>
This test program applies the function to each member of a list of numbers.
This test program applies the function to each member of a list of numbers.
<lang Ursala>#show+
<syntaxhighlight lang="ursala">#show+


test = roman* <1990,2008,1,2,64,124,1666,10001></lang>
test = roman* <1990,2008,1,2,64,124,1666,10001></syntaxhighlight>
{{out}}
{{out}}
<pre>MCMXC
<pre>MCMXC
Line 7,126: Line 7,126:
=={{header|Vala}}==
=={{header|Vala}}==
{{trans|D}}
{{trans|D}}
<lang vala>string to_roman(int n)
<syntaxhighlight lang="vala">string to_roman(int n)
requires (n > 0 && n < 5000)
requires (n > 0 && n < 5000)
{
{
Line 7,151: Line 7,151:
print("%s\n", to_roman(3456));
print("%s\n", to_roman(3456));
print("%s\n", to_roman(2488));
print("%s\n", to_roman(2488));
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 7,161: Line 7,161:


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Private Function roman(n As Integer) As String
<syntaxhighlight lang="vb">Private Function roman(n As Integer) As String
roman = WorksheetFunction.roman(n)
roman = WorksheetFunction.roman(n)
End Function
End Function
Line 7,169: Line 7,169:
Debug.Print roman(CInt(x)); " ";
Debug.Print roman(CInt(x)); " ";
Next x
Next x
End Sub</lang>{{out}}
End Sub</syntaxhighlight>{{out}}
<pre>X MMXVI DCCC MMDCCLXIX MDCLXVI CDLXXVI MCDLIII </pre>
<pre>X MMXVI DCCC MMDCCLXIX MDCLXVI CDLXXVI MCDLIII </pre>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>// Main program for testing the function
<syntaxhighlight lang="vedit">// Main program for testing the function
//
//
do {
do {
Line 7,204: Line 7,204:
}
}
Buf_Quit(OK)
Buf_Quit(OK)
Return</lang>
Return</syntaxhighlight>


{{out}}
{{out}}
Line 7,216: Line 7,216:
{{trans|BASIC}}
{{trans|BASIC}}


<lang vb>Function toRoman(value) As String
<syntaxhighlight lang="vb">Function toRoman(value) As String
Dim arabic As Variant
Dim arabic As Variant
Dim roman As Variant
Dim roman As Variant
Line 7,237: Line 7,237:
Sub Main()
Sub Main()
MsgBox toRoman(Val(InputBox("Number, please")))
MsgBox toRoman(Val(InputBox("Number, please")))
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>var romans = [
<syntaxhighlight lang="ecmascript">var romans = [
[1000, "M"],
[1000, "M"],
[900, "CM"],
[900, "CM"],
Line 7,272: Line 7,272:
System.print(encode.call(1666))
System.print(encode.call(1666))
System.print(encode.call(2008))
System.print(encode.call(2008))
System.print(encode.call(2020))</lang>
System.print(encode.call(2020))</syntaxhighlight>


{{out}}
{{out}}
Line 7,285: Line 7,285:
{{trans|DWScript}}
{{trans|DWScript}}
{{works with|Windows XBasic}}
{{works with|Windows XBasic}}
<lang xbasic>
<syntaxhighlight lang="xbasic">
PROGRAM "romanenc"
PROGRAM "romanenc"
VERSION "0.0000"
VERSION "0.0000"
Line 7,344: Line 7,344:
END FUNCTION
END FUNCTION
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 7,353: Line 7,353:


=={{header|XLISP}}==
=={{header|XLISP}}==
<lang lisp>(defun roman (n)
<syntaxhighlight lang="lisp">(defun roman (n)
(define roman-numerals '((1000 "m") (900 "cm") (500 "d") (400 "cd") (100 "c") (90 "xc") (50 "l") (40 "xl") (10 "x") (9 "ix") (5 "v") (4 "iv") (1 "i")))
(define roman-numerals '((1000 "m") (900 "cm") (500 "d") (400 "cd") (100 "c") (90 "xc") (50 "l") (40 "xl") (10 "x") (9 "ix") (5 "v") (4 "iv") (1 "i")))
(defun romanize (arabic-numeral numerals roman-numeral)
(defun romanize (arabic-numeral numerals roman-numeral)
Line 7,364: Line 7,364:


; test the function:
; test the function:
(display (mapcar roman '(10 2016 800 2769 1666 476 1453)))</lang>
(display (mapcar roman '(10 2016 800 2769 1666 476 1453)))</syntaxhighlight>
{{out}}
{{out}}
<pre>(x mmxvi dccc mmdcclxix mdclxvi cdlxxvi mcdliii)</pre>
<pre>(x mmxvi dccc mmdcclxix mdclxvi cdlxxvi mcdliii)</pre>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>proc Rom(N, A, B, C); \Display 1..9 in Roman numerals
<syntaxhighlight lang="xpl0">proc Rom(N, A, B, C); \Display 1..9 in Roman numerals
int N, A, B, C, I;
int N, A, B, C, I;
[case N of
[case N of
Line 7,393: Line 7,393:
for I:= 0 to 7 do
for I:= 0 to 7 do
[IntOut(0, Tbl(I)); Text(0, ". "); Roman(Tbl(I)); CrLf(0)];
[IntOut(0, Tbl(I)); Text(0, ". "); Roman(Tbl(I)); CrLf(0)];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 7,408: Line 7,408:


=={{header|XSLT}}==
=={{header|XSLT}}==
<lang xslt>
<syntaxhighlight lang="xslt">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/data/number">
<xsl:template match="/data/number">
Line 7,459: Line 7,459:
</xsl:template>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
</syntaxhighlight>
</lang>


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>roman$ = "M, CM, D, CD, C, XC, L, XL, X, IX, V, IV, I"
<syntaxhighlight lang="yabasic">roman$ = "M, CM, D, CD, C, XC, L, XL, X, IX, V, IV, I"
decml$ = "1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1"
decml$ = "1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1"
Line 7,492: Line 7,492:
// 2009 = MMIX
// 2009 = MMIX
// 1666 = MDCLXVI
// 1666 = MDCLXVI
// 3888 = MMMDCCCLXXXVIII</lang>
// 3888 = MMMDCCCLXXXVIII</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>var [const] romans = L(
<syntaxhighlight lang="zkl">var [const] romans = L(
L("M", 1000), L("CM", 900), L("D", 500), L("CD", 400), L("C", 100),
L("M", 1000), L("CM", 900), L("D", 500), L("CD", 400), L("C", 100),
L("XC", 90), L("L", 50), L("XL", 40), L("X", 10), L("IX", 9),
L("XC", 90), L("L", 50), L("XL", 40), L("X", 10), L("IX", 9),
Line 7,503: Line 7,503:
foreach R,N in (romans){ text += R*(i/N); i = i%N; }
foreach R,N in (romans){ text += R*(i/N); i = i%N; }
return(text);
return(text);
}</lang>
}</syntaxhighlight>
<pre>
<pre>
toRoman(1990) //-->"MCMXC"
toRoman(1990) //-->"MCMXC"
Line 7,511: Line 7,511:


=={{header|Zoea}}==
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
<lang Zoea>
program: decimal_roman
program: decimal_roman
input: 12
input: 12
output: 'XII'
output: 'XII'
</syntaxhighlight>
</lang>


=={{header|Zoea Visual}}==
=={{header|Zoea Visual}}==
Line 7,522: Line 7,522:
=={{header|Zsh}}==
=={{header|Zsh}}==
Based on the python solution.
Based on the python solution.
<lang zsh>function printroman () {
<syntaxhighlight lang="zsh">function printroman () {
local -a conv
local -a conv
local number=$1 div rom num out
local number=$1 div rom num out
Line 7,533: Line 7,533:
done
done
echo $out
echo $out
}</lang>
}</syntaxhighlight>