commit ea62a9518dd625ec98d9ccb06173b3fb9dfecf6e Author: Aritra Banik Date: Sun Feb 2 02:21:56 2025 +0530 001 diff --git a/roman_numeral_encoder.v b/roman_numeral_encoder.v new file mode 100644 index 0000000..e78410e --- /dev/null +++ b/roman_numeral_encoder.v @@ -0,0 +1,85 @@ +import readline + +fn main() { + mut r := readline.Readline{} + answer := r.read_line('What is your number: ')! + + symbols := { + 1: 'I' + 5: 'V' + 10: 'X' + 50: 'L' + 100: 'C' + 500: 'D' + 1000: 'M' + } + + a := answer.int() + if a == 0 { + panic('The value cannot be 0 or a string') + } + + mut num := a + mut result := '' + + // Handle thousands + mut m := num / 1000 + for i := 0; i < m; i++ { + result += symbols[1000] + } + num = num % 1000 + + // Handle hundreds + mut c := num / 100 + if c == 9 { + result += symbols[100] + symbols[1000] + } else if c >= 5 { + result += symbols[500] + for i := 0; i < c - 5; i++ { + result += symbols[100] + } + } else if c == 4 { + result += symbols[100] + symbols[500] + } else { + for i := 0; i < c; i++ { + result += symbols[100] + } + } + num = num % 100 + + // Handle tens + mut x := num / 10 + if x == 9 { + result += symbols[10] + symbols[100] + } else if x >= 5 { + result += symbols[50] + for i := 0; i < x - 5; i++ { + result += symbols[10] + } + } else if x == 4 { + result += symbols[10] + symbols[50] + } else { + for i := 0; i < x; i++ { + result += symbols[10] + } + } + num = num % 10 + + // Handle ones + if num == 9 { + result += symbols[1] + symbols[10] + } else if num >= 5 { + result += symbols[5] + for i := 0; i < num - 5; i++ { + result += symbols[1] + } + } else if num == 4 { + result += symbols[1] + symbols[5] + } else { + for i := 0; i < num; i++ { + result += symbols[1] + } + } + + println('${a} --> ${result}') +}