Pages

Search This Blog

Summation of elements of an array Using Function



Module SumArFunc

    Sub Main()
        Dim n, i, s As Integer
        Console.Write("Enter Number of elements: ")
        n = CInt(Console.ReadLine())
        Dim arr(n) As Integer
        Console.WriteLine()
        For i = 0 To n - 1
            Console.Write("Enter Element (" + (i + 1).ToString() + ")= ")
            arr(i) = CInt(Console.ReadLine())
        Next
        Console.WriteLine()
        Console.WriteLine("Array")
        Console.WriteLine("-------------")
        For i = 0 To n - 1
            Console.WriteLine("Array (" & i & ")= " & arr(i))
        Next
        Console.WriteLine("-------------")
        s = sum(arr, n)
        Console.WriteLine("Sum of Elements= " & s.ToString())
        Console.ReadLine()

    End Sub
    Function sum(ByVal x() As Integer, ByVal y As Integer)
        Dim j, s1 As Integer
        s1 = 0
        For j = 0 To y - 1
            s1 += x(j)
        Next
        Return s1

    End Function

End Module

1 comment: