1// Visit => https://duniya-roman-numeral-converter.netlify.app/
2// Npm Package => https://www.npmjs.com/package/cr-numeral
3
4const convertToRoman = num => {
5 const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
6 const roman = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
7 let romanNumeral = ''
8
9 // While num is not equal to 0, keep iterating with the value
10 while(num !== 0){
11 // Find from the numbers array the match for the current number
12 const index = numbers.findIndex(nums => num >= nums)
13
14 // Keeping pushing the roman value to romanNumeral
15 // Cause the found number from numbers matches the index of its
16 // Corresponding roman below
17 romanNumeral += roman[index]
18
19 // Set num to a new value by Substracting the used number from it
20 num -= numbers[index]
21 }
22
23 return romanNumeral
24}
25
26convertToRoman(3999);
27
28// With love @kouqhar
1romans= [ ['M','D','C'] ,['C','L','X'], ['X','V','I'] ]
2
3
4num = int(input())
5st =""
6div = 100
7t=num
8z =t//1000
9t = t%1000
10if (z>0) :
11 st = st+ z* 'M'
12
13for aplha in romans:
14 z = t//div
15 t%=div
16 if (z in range(1,4)):
17 st+= z*aplha[2]
18 elif (z in range (6,9)):
19 st+= aplha[1] + (z-5)*aplha[2]
20 elif z==4:
21 st+= aplha[2] + aplha[1]
22 elif z==5:
23 st+= aplha[1]
24 elif z==9:
25 st+= aplha[2] + aplha[1]
26
27 div//=10
28
29print(st)
1// Roman numeral conversions
2fn int_to_roman(num: i32) -> String {
3 let m = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
4 let s = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
5
6 let (mut num, mut buf) = (num, vec![]);
7 for i in 0..13 {
8 let mut j = num / m[i];
9 num %= m[i];
10 while j > 0 {
11 buf.push(s[i]);
12 j -= 1;
13 }
14 }
15 buf.into_iter().collect()
16}
17
18fn roman_to_int1(s: String) -> i32 {
19 s.chars().rev().fold((0, 0), |(sum, prev), c| {
20 let n = match c {
21 'I' => 1,
22 'V' => 5,
23 'X' => 10,
24 'L' => 50,
25 'C' => 100,
26 'D' => 500,
27 'M' => 1000,
28 _ => panic!("Not a roman numeral")};
29 if n >= prev {
30 (sum + n, n)
31 } else {
32 (sum - n, n)
33 }
34 }).0
35}
36
37fn main() {
38 let i = 2021;
39 println!("Int {} to Roman {} to Int {}", i, int_to_roman(i), roman_to_int1(int_to_roman(i)));
40}
1/*
2 // This application helps you convert roman numerals to numbers or vice-versa
3*/
4
5// First install the package @ "npm install cr-numeral"
6// Then import or require the package in your application
7const {
8 convertNumberToRoman: cnr,
9 convertRomanToNumber: crn,
10} = require("cr-numeral");
11// OR
12const cnr = require("cr-numeral").convertNumberToRoman;
13const crn = require("cr-numeral").convertRomanToNumber;
14
15// Define your variables
16const number = 2021;
17const numeral = "MMMXXV"; // Case-insensitive
18
19// Use your package/module
20const toRoman = cnr(number);
21const toNumber = crn(numeral);
22
23// Log or use your result
24console.log(toRoman, toNumber);
25
26 // Converting a number to Roman Numeral
27const { convertNumberToRoman } = require('cr-numeral');
28// OR
29const convertNumberToRoman = require('cr-numeral').convertNumberToRoman;
30
31convertNumberToRoman(2021));
32"MMXXI"
33
34convertNumberToRoman(-2021)); // Can not convert a negative number or zero
35"Can not convert Zero or negative numbers!!!"
36
37convertNumberToRoman("na256m"));
38"You must provide only valid numbers!!!"
39
40convertNumberToRoman(false));
41"Cannot use Boolean values!!!"
42
43convertNumberToRoman(true));
44"Cannot use Boolean values!!!"
45
46 // Converting Roman Numeral to Number
47const { convertRomanToNumber } = require('cr-numeral');
48// OR
49const convertRomanToNumber = require('cr-numeral').convertRomanToNumber;
50
51convertRomanToNumber("MMXXI"));
52"2021"
53
54convertRomanToNumber("na256m"));
55"Provide a valid roman character!!!"
56"Cause these are invalid roman numerals : [ N,A,2,5,6 ]"
57
58convertRomanToNumber(6355));
59"You must provide only valid strings!!!"
60
61convertRomanToNumber(false));
62"Cannot use Boolean values!!!"
63
64convertRomanToNumber(true));
65"Cannot use Boolean values!!!"
1function convertToRoman() {
2 let arabic = document.getElementById('arabicNumeral').value; // input value
3 let roman = ''; // variable that will hold the result
4}
1//Java Implementation of Roman To Number
2
3
4public class RomanToNumber {
5 public static int declareIntOfChar(char c)
6 {
7 int val=0;
8 switch(c)
9 {
10 case 'I':
11 val=1;
12 break;
13 case 'V':
14 val=5;
15 break;
16 case 'X':
17 val=10;
18 break;
19 case 'L' :
20 val=50;
21 break;
22 case 'C' :
23 val=100;
24 break;
25 case 'D' :
26 val=500;
27 break;
28 case 'M' :
29 val=1000;
30 break;
31 default :
32 val=-1;
33 break;
34 }
35 return val;
36 }
37 public static void main(String[] args) {
38 String s = "XCV";
39 int sum = 0,c1,c2;
40 for(int i=0;i<s.length();i++)
41 {
42 c1=declareIntOfChar(s.charAt(i));
43 if(i+1<s.length())
44 {
45 c2=declareIntOfChar(s.charAt(i+1));
46 if(c1<c2)
47 {
48 sum = sum + c2 - c1;
49 i++;
50 }
51 else
52 {
53 sum = sum + c1;
54 }
55 }
56 else
57 {
58 sum = sum + c1;
59 }
60 }
61 System.out.print(s + " = " + sum);
62 }
63}
64