Answers for "numbers to roman numerals"

1

convert decimal to roman numerals

function convertToRoman(num) 
{
  let 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"
  }
  let roman = "";
  let romankeys = Object.keys(map).reverse();
  romankeys.forEach((keys) => {
      while(keys <= num)
      {
        roman += map[keys];
        num -= keys;
      }
        
  });
  return roman;
}

//https://www.rapidtables.com/convert/number/how-number-to-roman-numerals.html (The refrence for the Roman Calculator)
console.log(convertToRoman(20));
Posted by: Guest on July-10-2021
0

number to roman numeral converter

romans= [ ['M','D','C'] ,['C','L','X'], ['X','V','I'] ]


num = int(input())
st =""
div = 100
t=num
z =t//1000
t = t%1000
if (z>0) :
  st = st+ z* 'M'

for aplha in romans:
  z = t//div
  t%=div
  if (z in range(1,4)):
    st+= z*aplha[2]
  elif (z in range (6,9)):
    st+= aplha[1] + (z-5)*aplha[2]
  elif z==4:
    st+= aplha[2] + aplha[1]
  elif z==5:
    st+= aplha[1]
  elif z==9:
    st+= aplha[2] + aplha[1]
  
  div//=10

print(st)
Posted by: Guest on October-27-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 numbers to numbers

//Java Implementation of Roman To Number


public class RomanToNumber {
	public static int declareIntOfChar(char c)
	{
		int val=0;
		switch(c)
		{
		case 'I':
			val=1;
			break;
		case 'V':
			val=5;
			break;
		case 'X':
			val=10;
			break;
		case 'L' : 
			val=50;
			break;
		case 'C' : 
			val=100;
			break;
		case 'D' : 
			val=500;
			break;
		case 'M' : 
			val=1000;
			break;
		default :
			val=-1;	
			break;
		}
		return val;
	}
	public static void main(String[] args) {
		String s = "XCV";
		int sum = 0,c1,c2;
		for(int i=0;i<s.length();i++)
		{
			c1=declareIntOfChar(s.charAt(i));
			if(i+1<s.length())
			{
				c2=declareIntOfChar(s.charAt(i+1));
				if(c1<c2)
				{
					sum = sum + c2 - c1;
					i++;
				}
				else
				{
					sum = sum + c1;
				}
			}
			else
			{
				sum = sum + c1;
			}
		}
		System.out.print(s + " = " + sum);
	}
}
Posted by: Guest on January-30-2021

Code answers related to "numbers to roman numerals"

Browse Popular Code Answers by Language