Pages

Wednesday, 27 November 2024

Number to Roman Converter Tool

Number to Roman Converter

Number to Roman Converter

Your Roman numeral will appear here.

/* General Reset */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Body Styling */ body { font-family: 'Arial', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: linear-gradient(to right, #6a11cb, #2575fc); color: #fff; } /* Container */ .container { text-align: center; background: rgba(255, 255, 255, 0.1); padding: 30px; border-radius: 15px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); width: 90%; max-width: 400px; } /* Heading */ .container h1 { font-size: 2rem; margin-bottom: 20px; } /* Converter Input Section */ .converter { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; } /* Input Styling */ .converter input { padding: 10px; font-size: 1rem; border: none; border-radius: 5px; outline: none; text-align: center; } /* Button Styling */ .converter button { padding: 10px; font-size: 1rem; font-weight: bold; border: none; border-radius: 5px; background-color: #ff7e5f; color: #fff; cursor: pointer; transition: background 0.3s ease-in-out; } .converter button:hover { background-color: #e75134; } /* Output Styling */ .output { font-size: 1.2rem; font-weight: bold; background: rgba(255, 255, 255, 0.15); padding: 15px; border-radius: 5px; } /* Responsive Design */ @media (max-width: 600px) { .container h1 { font-size: 1.5rem; } .converter input, .converter button { font-size: 0.9rem; } .output { font-size: 1rem; } } // Number to Roman Converter Logic function convertToRoman(num) { const romanNumerals = [ { value: 1000, numeral: 'M' }, { value: 900, numeral: 'CM' }, { value: 500, numeral: 'D' }, { value: 400, numeral: 'CD' }, { value: 100, numeral: 'C' }, { value: 90, numeral: 'XC' }, { value: 50, numeral: 'L' }, { value: 40, numeral: 'XL' }, { value: 10, numeral: 'X' }, { value: 9, numeral: 'IX' }, { value: 5, numeral: 'V' }, { value: 4, numeral: 'IV' }, { value: 1, numeral: 'I' } ]; let roman = ''; romanNumerals.forEach(({ value, numeral }) => { while (num >= value) { roman += numeral; num -= value; } }); return roman; } // DOM Manipulation document.getElementById('convertBtn').addEventListener('click', () => { const numberInput = document.getElementById('numberInput').value; const outputElement = document.getElementById('romanOutput'); if (numberInput < 1 || numberInput > 3999 || isNaN(numberInput)) { outputElement.textContent = "Please enter a number between 1 and 3999."; outputElement.style.color = "#ff5252"; } else { const romanNumeral = convertToRoman(parseInt(numberInput)); outputElement.textContent = `Roman Numeral: ${romanNumeral}`; outputElement.style.color = "#32cd32"; } });