Roman Numerals
Converts a number from 1 to 3999 into Roman numerals.
- Level
- Advanced
- Topic:
- Math
- Concepts:
- Input & output
- Arrays
- Para
- Mientras
- Text
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
Two parallel arrays hold the values and their symbols. A Mientras loop inside a Para subtracts the largest possible value as many times as it fits.
Code
Algoritmo NumerosRomanos Definir valores, numero, resto, i Como Entero Definir simbolos, romano Como Caracter Dimension valores[13], simbolos[13] valores[1] <- 1000 simbolos[1] <- "M" valores[2] <- 900 simbolos[2] <- "CM" valores[3] <- 500 simbolos[3] <- "D" valores[4] <- 400 simbolos[4] <- "CD" valores[5] <- 100 simbolos[5] <- "C" valores[6] <- 90 simbolos[6] <- "XC" valores[7] <- 50 simbolos[7] <- "L" valores[8] <- 40 simbolos[8] <- "XL" valores[9] <- 10 simbolos[9] <- "X" valores[10] <- 9 simbolos[10] <- "IX" valores[11] <- 5 simbolos[11] <- "V" valores[12] <- 4 simbolos[12] <- "IV" valores[13] <- 1 simbolos[13] <- "I" Escribir "Número entre 1 y 3999:" Leer numero Si numero < 1 O numero > 3999 Entonces Escribir "Los romanos no escribían ese número." SiNo romano <- "" resto <- numero // Restamos el mayor valor posible tantas veces como entre Para i <- 1 Hasta 13 Hacer Mientras resto >= valores[i] Hacer romano <- romano + simbolos[i] resto <- resto - valores[i] FinMientras FinPara Escribir numero, " en números romanos es ", romano FinSiFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This is what the program shows when it runs. The answers typed by the user are highlighted.
Número entre 1 y 3999:User answer: 20242024 en números romanos es MMXXIV
Challenge
Also show how many symbols the Roman numeral has.
Show hint
Use Longitud on the romano variable.
Related examples
Shell Sort
Insertion sort between far-apart elements, with a gap that shrinks every pass.
AdvancedMerge Sort
Splits the array in halves, sorts each one recursively and merges them.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
Advanced