Convert Positive Integers to Roman Numbers in JavaScript



We have to write a JavaScript program to generate the Roman number representation of given integer numbers. The code should accept a positive integer as input and will be able to transform the integer into its equivalent Roman number.

Example Scenario:

Input: num = 10;
Output: roman_num = X

How to Convert Integers to Roman Numbers

To solve the given problem, define a function to convert an integer to roman. This function will take a positive integer as input and will return the corresponding Roman numeral as a string. It will use an array of objects to map the integer numbers to the Roman number.

The defined function will iterate through the array. The iteration process will start from the largest value to the smallest value. During each iteration, it will check if the given number is greater than or equal to the current value of the roman symbols. If so, the function will append the corresponding symbol to the Roman number string and subtract the value from the given input number. This process will run until the given input number is less than the current value. Once the loop is finished the program will generate the Roman number representation of the input number.

Using for Loop

In this approach, we implement the above discussed logic using the for loop. In JavaScript, a for loop is used to execute a block of code repeatedly, until a specified condition evaluates to false. Use this loop when the number of iterations is fixed and known.

Example

A JavaScript program that illustrates how to convert positive integers to Roman numbers using for loop.

//function for converting integer to roman
function intToRoman(num) {
   if (typeof num !== 'number' || num <= 0 || num > 3999) {
      return 'Invalid number';
   }

   const romanSymbols = [
      { value: 1000, symbol: 'M' },
      { value: 900, symbol: 'CM' },
      { value: 500, symbol: 'D' },
      { value: 400, symbol: 'CD' },
      { value: 100, symbol: 'C' },
      { value: 90, symbol: 'XC' },
      { value: 50, symbol: 'L' },
      { value: 40, symbol: 'XL' },
      { value: 10, symbol: 'X' },
      { value: 9, symbol: 'IX' },
      { value: 5, symbol: 'V' },
      { value: 4, symbol: 'IV' },
      { value: 1, symbol: 'I' }
   ];

   let romanNumber = '';

   for (let i = 0; i < romanSymbols.length; i++) {
      while (num >= romanSymbols[i].value) {
      romanNumber += romanSymbols[i].symbol;
      num -= romanSymbols[i].value;
      }
   }

   return romanNumber;
}

console.log("The roman numbers for the given integers")
console.log(intToRoman(-10));
console.log(intToRoman(46));
console.log(intToRoman(1995));
console.log(intToRoman(5000));

Output of the above code is ?

The roman numbers for the given integers
Invalid number
XLVI
MCMXCV
Invalid number

Complexity

The time complexity of the code is linear O(n), where, n is the input number. And, the space complexity for the code is constant as O(1) as we are using a fixed amount of additional space besides the input size.

Using Recursion

Recursion, a programming approach where a function calls itself to solve a given problem, is another way to implement the logic discussed above in this article.

Example

The below JavaScript program uses recursion to convert positive integers to Roman number.

function intToRoman(num, romanNumber = '', i = 0) {
   const romanSymbols = [
      { value: 1000, symbol: 'M' },
      { value: 900, symbol: 'CM' },
      { value: 500, symbol: 'D' },
      { value: 400, symbol: 'CD' },
      { value: 100, symbol: 'C' },
      { value: 90, symbol: 'XC' },
      { value: 50, symbol: 'L' },
      { value: 40, symbol: 'XL' },
      { value: 10, symbol: 'X' },
      { value: 9, symbol: 'IX' },
      { value: 5, symbol: 'V' },
      { value: 4, symbol: 'IV' },
      { value: 1, symbol: 'I' }
   ];

   if (typeof num !== 'number' || num < 0 || num > 3999) {
      return 'Invalid number';
   }
   if (num === 0) {
      return romanNumber;
   }

   while (num >= romanSymbols[i].value) {
      romanNumber += romanSymbols[i].symbol;
      num -= romanSymbols[i].value;
   }
   // recursive call
   return intToRoman(num, romanNumber, i + 1);
}

console.log("The roman numbers for the given integers");
console.log(intToRoman(-10));
console.log(intToRoman(46)); 
console.log(intToRoman(1995));
console.log(intToRoman(5000)); 

The above code will produce following result ?

The roman numbers for the given integers
Invalid number
XLVI
MCMXCV
Invalid number
Updated on: 2024-09-30T16:05:43+05:30

657 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements