Pages

lunes, 21 de enero de 2013

Convertir a números romanos entre 1000 y 2000 Codigo Fuente en Visual Basic

Problema
Determinar un algoritmo que permita convertir un número arábigo  comprendido entre  1000 y 2000 a números romanos

Solución
  • Entrada: 1597
  • Salida:  MDXCVII
Codigo Fuente en Visual Basic
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim N, u, d, c, m As Integer
        Dim Unidad() As String = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
        Dim Decena() As String = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
        Dim Centena() As String = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
        Dim Mil() As String = {"", "M", "MM"}
        N = TextBox1.Text
        u = N Mod 10
        d = Math.Floor(N / 10) Mod 10
        c = Math.Floor(N / 100) Mod 10
        m = Math.Floor(N / 1000)
        If (N >= 1000 And N <= 2000) Then
            TextBox2.Text = Mil(m) + Centena(c) + Decena(d) + Unidad(u)
        Else
            TextBox2.Text = "Fuera de Rango"
        End If
    End Sub
End Class

0 comentarios:

Publicar un comentario