Procedimientos y Funciones en VB .NET
ejercicio 1:
Escriba un procedimiento que imprima la hora actual.
Código VB .NET:
Module Module1
Sub Main()
hora()
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Sub hora()
Console.WriteLine("Hora actual: " & TimeOfDay)
End Sub
End Module
ejercicio 2:
Escriba una aplicación que capture un texto por teclado y que posea un procedimiento para mostrar el texto capturado en mayúsculas y color rojo y otro procedimiento para mostrarlo en minúsculas y color amarillo.
Código VB .NET:
Module Module2
Sub Main()
Dim texto As String
Console.WriteLine("Ingrese un texto:")
texto = Console.ReadLine()
Console.WriteLine()
RojoUp(texto)
AmaLow(texto)
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Sub RojoUp(ByVal texto As String)
Console.Write("Rojo y Mayúsculas: ")
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(texto.ToUpper)
End Sub
--------------------------------------------------------------------------------------------------------
Sub AmaLow(ByVal texto As String)
Console.ForegroundColor = ConsoleColorGray
Console.Write("Amarillo y Minusculas: ")
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine(texto.ToLower)
End Sub
End Module
ejercicio 3:
Escriba una aplicación para capturar el número de teléfono de 5 participantes y que posea un procedimiento que seleccione e imprima de forma aleatoria el número de teléfono ganador.
Código VB .NET:
Module Module3
Sub Main()
Console.WriteLine("Prueba tu suerte!")
Console.WriteLine("Ingrese 5 números de telefono")
Seleccion()
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Sub Seleccion()
Dim tel(4) As String
For i = 0 To 4
tel(i) = Console.ReadLine()
Next
Dim re As New Random()
Dim n As Integer
n = re.Next(0, 5)
Console.WriteLine()
Console.Write("El telefono ganador es: " & tel(n))
End Sub
End Module
ejercicio 4:
Escriba una función para calcular el valor a pagar a un empleado por servicios, capturando por teclado el nombre y horas trabajadas, estableciendo el valor de la hora a $ 6.50
Código VB .NET:
Module Module4
Sub Main()
Dim hora As Decimal
Console.WriteLine("Monto a pagar a un empleado por horas trabajadas")
Sub Main()
Dim hora As Decimal
Console.WriteLine("Monto a pagar a un empleado por horas trabajadas")
Console.Write("Ingrese la cantidad de horas: ")
hora = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Monto a pagar: $" & sueldo(hora))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function sueldo(x As Integer) As Decimal
Return x * 6.5
End Function
End Module
ejercicio 5:
ejercicio 5:
Escriba una función que indique si un año capturado por teclado es o no es bisiesto.
Código VB .NET:
Module Module5
Sub Main()
Dim año As Integer
Console.WriteLine("Compruebe si un año es bisiesto")
Console.Write("Ingrese un año: ")
año = Console.ReadLine()
Console.WriteLine()
Console.WriteLine(bisiesto(año))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function bisiesto(x As Integer)
If (x Mod 4 = 0) And ((x Mod 100 <> 0) Or (x Mod 400 = 0)) Then
Return "El año es bisiesto"
Else
Return "El año no es bisiesto" End If
End Function
End Module
Sub Main()
Dim año As Integer
Console.WriteLine("Compruebe si un año es bisiesto")
Console.Write("Ingrese un año: ")
año = Console.ReadLine()
Console.WriteLine()
Console.WriteLine(bisiesto(año))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function bisiesto(x As Integer)
If (x Mod 4 = 0) And ((x Mod 100 <> 0) Or (x Mod 400 = 0)) Then
Return "El año es bisiesto"
Else
Return "El año no es bisiesto" End If
End Function
End Module
ejercicio 6:
Escriba una función que retorne el mayor de dos números ingresados por teclado.
Código VB .NET:
Module Module6
Sub Main()
Dim num1, num2 As Integer
Console.WriteLine("¿Qué número es mayor?")
Console.WriteLine("Ingrese dos numeros:")
num1 = Console.ReadLine
num2 = Console.ReadLine
Console.WriteLine()
Console.Write("El número mayor es: " & mayor(num1, num2))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function mayor(x As Integer, y As Integer)
If x > y Then
Return x
Else
Return y
End If
End Function
End Module
ejercicio 7:
Escriba una función que retorne si un número capturado por teclado es par o impar.
Código VB .NET:
Module Module7
Sub Main()
Dim k As Integer
Console.WriteLine("Descubra si un número es par o impar")
Console.Write("Introduzca un número: ")
k = Console.ReadLine()
Console.WriteLine()
Console.Write("Él número es " & ParImpar(k))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Sub Main()
Dim k As Integer
Console.WriteLine("Descubra si un número es par o impar")
Console.Write("Introduzca un número: ")
k = Console.ReadLine()
Console.WriteLine()
Console.Write("Él número es " & ParImpar(k))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
If x Mod 2 = 0 Then
Return "par"
Else
Return "impar"
End If
End Function
End Module
Return "par"
Else
Return "impar"
End If
End Function
End Module
ejercicio 8:
Escriba una función que retorne el factorial de un número capturado por teclado.
Código VB .NET:
Module Module8
Sub Main()
Dim num As Integer
Console.WriteLine("Calcule el factorial de un número")
Console.Write("Introduzca un número: ")
num = Integer.Parse(Console.ReadLine())
Console.WriteLine()
Console.WriteLine(num & "! = " & facto(num))
Console.Read()
End Sub
--------------------------------------------------------------------------------------------------------
Function facto(ByVal num) As Integer
If num = 1 Then
Return num
Else
Return num * facto(num - 1)
End If
End Function
End Module
ejercicio 9:
Escriba una aplicación para calcular la nota final de un alumno, utilizando el sistema de evaluación de la materia programación I, crear una sola función que permita calcular la nota promedio de cada periodo.
Código VB .NET:
Module Module9
Sub Main()
Dim nota1, nota2, nota3, prom1, prom2, prom3, final As Decimal
Console.WriteLine("Calcule la nota final de un alumno:")
Console.WriteLine()
Console.WriteLine("PERIODO 1")
Console.WriteLine("Ingrese nota 1")
nota1 = Console.ReadLine()
Console.WriteLine("Ingrese nota 2")
nota2 = Console.ReadLine()
Console.WriteLine("Ingrese nota 3")
nota3 = Console.ReadLine()
prom1 = prom(nota1, nota2, nota3)
Console.Clear()
'---------------------------------------------
Console.WriteLine("PERIODO 2")
Console.WriteLine("Ingrese nota 1")
nota1 = Console.ReadLine()
Console.WriteLine("Ingrese nota 2")
nota2 = Console.ReadLine()
Console.WriteLine("Ingrese nota 3")
nota3 = Console.ReadLine()
prom2 = prom(nota1, nota2, nota3)
Console.Clear()
'---------------------------------------------
Console.WriteLine("PERIODO 3")
Console.WriteLine("Ingrese nota 1")
nota1 = Console.ReadLine()
Console.WriteLine("Ingrese nota 2")
nota2 = Console.ReadLine()
Console.WriteLine("Ingrese nota 3")
nota3 = Console.ReadLine()
prom3 = prom(nota1, nota2, nota3)
Console.Clear()
Console.WriteLine("RESULTADOS")
Console.WriteLine()
Console.WriteLine("Periodo 1: " & prom1)
Console.WriteLine("Periodo 2: " & prom2)
Console.WriteLine("Periodo 3: " & prom3)
final = (prom1 + prom2 + prom3) / 3
Console.WriteLine()
Console.WriteLine("Nota final: " & FormatNumber(final, 1))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function prom(x As Decimal, y As Decimal, z As Decimal)
Dim total As Decimall
total = ((x * 0.25) + (y * 0.25) + (z * 0.5)
Return FormatNumber(total, 1)
End Function
End Module
ejercicio 10:
Escribir una aplicación que capture por teclado 4 números y posea 3 funciones de nombre sumar pero que la primera permita sumar 2 números, la segunda 3 y la cuarta 4 números.
Código VB .NET:
Module Module10
Sub Main()
Dim n1, n2, n3, n4 As Integer
Console.WriteLine("Ingrese números a sumar")
n1 = Console.ReadLine()
n2 = Console.ReadLine()
n3 = Console.ReadLine()
n4 = Console.ReadLine()
Console.WriteLine()
Console.WriteLine("Probando la función:")
Console.WriteLine("Sumando 4 números: " & sumar(n1, n2, n3, n4))
Console.WriteLine("Sumando 3 números: " & sumar(n1, n2, n3))
Console.WriteLine("Sumando 2 números: " & sumar(n1, n2))
Console.ReadLine()
Sub Main()
Dim n1, n2, n3, n4 As Integer
Console.WriteLine("Ingrese números a sumar")
n1 = Console.ReadLine()
n2 = Console.ReadLine()
n3 = Console.ReadLine()
n4 = Console.ReadLine()
Console.WriteLine()
Console.WriteLine("Probando la función:")
Console.WriteLine("Sumando 4 números: " & sumar(n1, n2, n3, n4))
Console.WriteLine("Sumando 3 números: " & sumar(n1, n2, n3))
Console.WriteLine("Sumando 2 números: " & sumar(n1, n2))
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------------
Function sumar(ByVal x As Integer, ByVall y As Integer) As Integer
Return x + y
End Function
Return x + y
End Function
--------------------------------------------------------------------------------------------------------
Function sumar(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
Return x + y + z
End Function
Return x + y + z
End Function
--------------------------------------------------------------------------------------------------------
Return x + y + z + a
End Function
End Module