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()
End Sub
--------------------------------------------------------------------------------------------------------
Function sumar(ByVal x As Integer, ByVall y As Integer) As Integer
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
--------------------------------------------------------------------------------------------------------
Function sumar(ByVal x As Integer, y As Integer, z As Integer, a As Integer) As Integer
Return x + y + z + a
End Function
End Module