Pages

miércoles, 30 de enero de 2013

Calcular la media aritmética Codigo en Visual Basic

Problema
Diseñar un algoritmo que permita determinar la media aritmética de una lista de números positivos.

Solución
  • Entrada: 15, 8, 5, 12
  • Salida: 10
Codigo Fuente en Visual Basic
Public Class Form1
    Dim b As Integer = 0
    Dim sum As Double = 0.0
    Dim cad As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim num As Integer
        num = TextBox1.Text
        If (num <> 0) Then
            sum = sum + num
            b = b + 1
            cad = cad + " " + Trim(num)
        End If
        TextBox2.Text = cad
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button2.Click
        Dim media As Double
        media = sum / b
        TextBox3.Text = Trim(media)
    End Sub
End Class

4 comentarios: