How to calculate the sum of all the odd numbers in one-dimensional int array in VB.NET

1 Answer

0 votes
Module Module1

    Sub Main()

        Dim arr() As Integer = {1, 2, 3, 4, 5, 6, 7}
        Dim sum As Integer

        For i As Integer = 0 To arr.Length - 1
            If (arr(i) Mod 2 <> 0) Then
                sum += arr(i)
            End If
        Next

        Console.WriteLine("sum = {0}", sum)

    End Sub

End Module

'run:
'
'sum = 16

 



answered Feb 8, 2016 by avibootz
edited Feb 17, 2016 by avibootz
...