33. JavaScript Program to convert given Integer into Roman Letter.
How does this program work?
- This program is used to convert given Integer into Roman Letter using JavaScript.
Here is the code
<html>
<head>
<title>JavaScript program to convert given Number into Roman letter</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter number"> </td>
</tr>
<tr>
<td> <button onclick="roman ()" >Submit</button> </td>
</tr>
</table>
<div id="num"></div>
</body>
<script type="text/javascript">
// Roman Letters
var roman_num = new Array();
roman_num = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
// Decimal number
var numbers = new Array();
numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
function Roman(value)
{
if (value <= 0 || value >= 4000)
return value;
var romanletter = "";
for (var i = 0; i< roman_num.length; i++)
{
while (value >= numbers[i])
{
value -= numbers[i];
romanletter += roman_num[i];
}
}
return romanletter;
}
function roman()
{
var n;
n = document.getElementById ("first").value;
document.getElementById ("num").innerHTML = "Roman letter for given number is : "+Roman(n);
}
</script>
</html>