# VB.Net program to calculate the sum of columns of MATRIX elements

Here, we are going to learn how to calculate the sum of columns of MATRIX elements in VB.Net?
Submitted by Nidhi, on December 11, 2020

Here, we will read a 2X3 matrix from the user then calculate the sum of column elements and then print the sum on the console screen.

Program/Source Code:

The source code to calculate the sum of columns of MATRIX elements is given below. The given program is compiled and executed successfully.

```'VB.Net program to calculate the sum of columns of MATRIX elements.

Module Module1
Sub Main()
Dim arr(,) As Integer = New Integer(2, 3) {}
Dim sum As Integer = 0

Console.WriteLine("Enter Matrix elements: ")
For i = 0 To 1 Step 1
For j = 0 To 2 Step 1
Console.Write("Enter element[{0}][{1}]: ", i, j)
Next
Next

Console.WriteLine("Matrix elements: ")
For i = 0 To 1 Step 1
For j = 0 To 2 Step 1
Console.Write("{0} ", arr(i, j))
Next
Console.WriteLine()
Next

For j = 0 To 2 Step 1
sum = 0
For i = 0 To 1 Step 1
sum = sum + arr(i, j)
Next
Console.WriteLine("Sum of Columns {0}: {1}", j, sum)
Next
End Sub

End Module
```

Output:

```Enter Matrix elements:
Enter element: 10
Enter element: 20
Enter element: 30
Enter element: 40
Enter element: 50
Enter element: 60
Matrix elements:
10 20 30
40 50 60
Sum of Columns 0: 50
Sum of Columns 1: 70
Sum of Columns 2: 90
Press any key to continue . . .
```

Explanation:

In the above program, we created a module Module1 that contains a function Main(). In the Main() function, we created a two-dimensional array of size 2X3.

```Console.WriteLine("Enter Matrix elements: ")
For i = 0 To 1 Step 1
For j = 0 To 2 Step 1
Console.Write("Enter element[{0}][{1}]: ", i, j)
Next
Next
```

In the above code, we read the elements of the matrix from the user, here the outer loop represents the rows of the matrix and the inner loop is used to represent columns of the matrix.

```Console.WriteLine("Matrix elements: ")
For i = 0 To 1 Step 1
For j = 0 To 2 Step 1
Console.Write("{0} ", arr(i, j))
Next
Console.WriteLine()
Next

For j = 0 To 2 Step 1
sum = 0
For i = 0 To 1 Step 1
sum = sum + arr(i, j)
Next
Console.WriteLine("Sum of Columns {0}: {1}", j, sum)
Next
```

In the above code, we calculated the sum of columns of Matrix elements and then printed the 2X3 matrix on the console screen.