Function sum of array Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function sum of array


Objetive:

Create a Visual Basic (VB.net) program to calculate the sum of the elements in an array. "Main" should be like this:

public static void Main()
{ int[] example = {20, 10, 5, 2 };
Console.WriteLine(
__"The sum of the example array is {0}", __Sum(example));
}
}


Code:

Imports System
Public Class exercise103
    Public Shared Function Sum(ByVal example As Integer()) As Integer
        Dim total As Integer = 0

        For i As Integer = 0 To example.Length - 1
            total += example(i)
        Next

        Return total
    End Function

    Public Shared Sub Main()
        Dim example As Integer() = {20, 10, 5, 2}
        Console.WriteLine("The sum of the example array is {0}", Sum(example))
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.