Program in VB.Net to count odd elements of a 2D array

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 183
Suppose we have a 2D array

{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
}

We need to find the count of odd elements of the 2D array. The below program will do so

Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication2
Class Program
Private Shared Sub Main(args As String())

Dim oddCounter As Integer = 0
Dim row As Integer = 4
Dim col As Integer = 4
Dim matrix As Integer(,) = New Integer(3, 3) {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
For i As Integer = 0 To row - 1
For j As Integer = 0 To col - 1
If matrix(i, j) Mod 2 <> 0 Then
oddCounter += 1
End If

Next
Next
Console.Write("Number of Odd Elements in the matrix : " & oddCounter)
Console.ReadKey()

End Sub
End Class
End Namespace

Result
--------
Number of Odd Elements in the matrix : 8

Comments or Responses

Login to post response