Answers for "Roman Numeral Converter"

5

Roman Numeral Converter

// Visit => https://duniya-roman-numeral-converter.netlify.app/
// Npm Package => https://www.npmjs.com/package/cr-numeral

const convertToRoman = num => {
    const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    const roman = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
    let romanNumeral = ''

    // While num is not equal to 0, keep iterating with the value
    while(num !== 0){
      	// Find from the numbers array the match for the current number
        const index = numbers.findIndex(nums => num >= nums)
        
        // Keeping pushing the roman value to romanNumeral
        // Cause the found number from numbers matches the index of its
        // Corresponding roman below
        romanNumeral += roman[index]
      
      	// Set num to a new value by Substracting the used number from it 
        num -= numbers[index]
    }

    return romanNumeral
}

convertToRoman(3999);

// With love @kouqhar
Posted by: Guest on February-27-2021
0

convert to roman number code

class py_solution:
    def int_to_Roman(self, num):
        val = [
            1000, 900, 500, 400,
            100, 90, 50, 40,
            10, 9, 5, 4,
            1
            ]
        syb = [
            "M", "CM", "D", "CD",
            "C", "XC", "L", "XL",
            "X", "IX", "V", "IV",
            "I"
            ]
        roman_num = ''
        i = 0
        while  num > 0:
            for _ in range(num // val[i]):
                roman_num += syb[i]
                num -= val[i]
            i += 1
        return roman_num


print(py_solution().int_to_Roman(1))
print(py_solution().int_to_Roman(4000))
Posted by: Guest on December-06-2020
0

roman numeral converter

function convertToRoman(num) {
  let json = {
    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"
  }
  let romanNumeral = "";
let  romanKeys= Object.keys(json).reverse();
  romanKeys.forEach(key =>{
    while(key <= num){
baby += json[key];
num -= key;
    }
  })
 return romanNumeral;
}

convertToRoman(29);
Posted by: Guest on June-16-2021
0

roman numeral conversions

// Roman numeral conversions
fn int_to_roman(num: i32) -> String {
    let m = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    let s = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];

    let (mut num, mut buf) = (num, vec![]);
    for i in 0..13 {
        let mut j = num / m[i];
        num %= m[i];
        while j > 0 {
            buf.push(s[i]);
            j -= 1;
        }
    }
    buf.into_iter().collect()
}

fn roman_to_int1(s: String) -> i32 {
    s.chars().rev().fold((0, 0), |(sum, prev), c| {
            let n = match c {
                'I' => 1,
                'V' => 5,
                'X' => 10,
                'L' => 50,
                'C' => 100,
                'D' => 500,
                'M' => 1000,
                _ => panic!("Not a roman numeral")};
            if n >= prev {              
                (sum + n, n)
            } else {
                (sum - n, n)
            }
        }).0
}

fn main() {
    let i = 2021;
    println!("Int {} to Roman {} to Int {}", i, int_to_roman(i), roman_to_int1(int_to_roman(i)));
}
Posted by: Guest on August-25-2021
0

roman numerals converter table embed

function convertToRoman() {
  let arabic = document.getElementById('arabicNumeral').value; // input value
  let roman = '';  // variable that will hold the result
}
Posted by: Guest on August-25-2020
0

roman numerals converter table embed

if (/^(0|[1-9]\d*)$/.test(arabic)) {
  // Regular expression tests
  if (arabic == 0) {
    // for decimal points and negative
    outputField.innerHTML = "Nulla"; // signs
  } else if (arabic != 0) {
    for (let i = 0; i < arabicArray.length; i++) {
      while (arabicArray[i] <= arabic) {
        roman += romanArray[i];
        arabic -= arabicArray[i];
      }
    }
    outputField.innerHTML = roman;
  }
} else {
  outputField.innerHTML =
    "Please enter non negative integers only. No decimal points.";
}
Posted by: Guest on August-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language