How To Create Converter For Decimal To Roman Numeral

Related Code: Converter For Decimal To Binary Related Code: Converter For Binary To Decimal If you are looking for another converter then you are at the right place and it’s called Converter For Decimal To Roman Numeral. The user input a value of Decimal Number to the textbox then the program will convert to Roman Numeral after the user clicks the Convert button. This program is very easy and you can do this also. Note: The input must be in the range of 1 to 4999, or I to MMMMCMXCIX. JavaScript Code This script will ask the user to enter a Decimal Number then the script also converts automatically into Roman Numeral.
  1. <script language="javascript" type="text/javascript">
  2. function convert_to_roman(val)
  3. {
  4. var roman_numerals=['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
  5. var list_numbers=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
  6. var x = parseInt(val);
  7.  
  8. if( x<1 || x>4999 )
  9. {
  10. alert(x+' number is out of range.');
  11. return 0;
  12. }
  13. display_result = '';
  14. for(a=12; a>=0;)
  15. if( list_numbers[a]<=x )
  16. {
  17. display_result += roman_numerals[a];
  18. x -= list_numbers[a];
  19. }
  20. else
  21. {
  22. a--;
  23. }
  24. return(display_result);
  25. }
  26. $(document).ready(function () {
  27. value_number = parseInt(prompt("Enter a Decimal Number to Convert into Roman Numeral"));
  28. document.write("<h3 style='text-align:center; margin-top: 100px;'><b style='color:blue;'>"+value_number + "</b> is equivalent to <b style='color:red;'>" +
  29. convert_to_roman(value_number) + "</b> in roman numerals. </h3>");
  30. });
  31. </script>
This is the output after the user types the Decimal Number. ResultRelated Code: Converter For Decimal To Binary Related Code: Converter For Binary To Decimal So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment