How to convert an integer to Roman numerals in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text

Module IntegerToRoman

    Function IntToRoman(num As Integer) As String
        ' Define values and their corresponding Roman numerals
        Dim values() As Integer = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
        Dim symbols() As String = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}

        Dim roman As New StringBuilder()

        ' Loop through values and subtract while possible
        For i As Integer = 0 To values.Length - 1
            While num >= values(i)
                num -= values(i)
                roman.Append(symbols(i))
            End While
        Next

        Return roman.ToString()
    End Function

    Sub Main()
        Console.WriteLine(IntToRoman(1994)) 
        Console.WriteLine(IntToRoman(196)) 
        Console.WriteLine(IntToRoman(9))    
    End Sub

End Module

	
' run:
'
' MCMXCIV
' CXCVI
' IX
' 

 



answered Dec 1, 2025 by avibootz
...