Number to Roman convert using Java Script
function convert(num) {
var result = '';
var rom = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var ara = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
for (var x = 0; x < rom.length; x++) {
while (num >= ara[x]) {
result += rom[x];
num -= ara[x];
}
}
return result;
}
Number to Roman convert using C#
internal static string ToRoman(int number)
{
StringBuilder result = new StringBuilder();
int[] digitsValues = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
string[] romanDigits = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" };
while (number > 0)
{
for (int i = digitsValues.Count() - 1; i >= 0; i--)
if (number / digitsValues[i] >= 1)
{
number -= digitsValues[i];
result.Append(romanDigits[i]);
break;
}
}
return result.ToString();
}
function convert(num) {
var result = '';
var rom = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var ara = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
for (var x = 0; x < rom.length; x++) {
while (num >= ara[x]) {
result += rom[x];
num -= ara[x];
}
}
return result;
}
Number to Roman convert using C#
internal static string ToRoman(int number)
{
StringBuilder result = new StringBuilder();
int[] digitsValues = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
string[] romanDigits = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" };
while (number > 0)
{
for (int i = digitsValues.Count() - 1; i >= 0; i--)
if (number / digitsValues[i] >= 1)
{
number -= digitsValues[i];
result.Append(romanDigits[i]);
break;
}
}
return result.ToString();
}
No comments:
Post a Comment