
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript algorithm for converting integers to roman numbers
Let’s say, we are required to write a function, say intToRoman(), which, as the name suggests, returns a Roman equivalent of the number passed in it as an argument.
Let’s write the code for this function −
Example
const intToRoman = (num) => { let result = ""; while(num){ if(num>=1000){ result += "M"; num -= 1000; }else if(num>=500){ if(num>=900){ result += "CM"; num -= 900; }else{ result += "D"; num -= 500; } }else if(num>=100){ if(num>=400){ result += "CD"; num -= 400; }else{ result += "C"; num -= 100; } }else if(num>=50){ if(num>=90){ result += "XC"; num -= 90; }else{ result += "L"; num -= 50; } }else if(num>=10){ if(num>=40){ result += "XL"; num -= 40; }else{ result += "X"; num -= 10; } }else if(num>=5){ if(num>=9){ result += "IX"; num -= 9; }else{ result += "V"; num -= 5; } }else{ if(num>=4){ result += "IV"; num -= 4; }else{ result += "I"; num -= 1; } } } return result; }; console.log(intToRoman(178)); console.log(intToRoman(89)); console.log(intToRoman(55)); console.log(intToRoman(1555));
Output
The output for this code in the console will be −
CLXXVIII LXXXIX LV MDLV
- Related Articles
- JavaScript algorithm for converting Roman numbers to decimal numbers
- JavaScript program to convert positive integers to roman numbers
- Converting numbers to Indian currency using JavaScript
- Converting strings to numbers with vanilla JavaScript
- Converting numbers to base-7 representation in JavaScript
- Algorithm for sorting array of numbers into sets in JavaScript
- Converting Roman Numerals to Decimal lying between 1 to 3999 in C++
- C program to convert roman numbers to decimal numbers
- Converting array of Numbers to cumulative sum array in JavaScript
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals in C++
- How to write 5000 in Roman Numbers?
- Converting all strings in list to integers in Python
- Converting numbers into corresponding alphabets and characters using JavaScript
- Algorithm for matrix multiplication in JavaScript
- Comparing integers by taking two numbers in JavaScript

Advertisements