# Diagonal elements of matrix 0 using .NET

This a resource based on the concept of mathematics.This program takes matrix values at run time from user and making the diagonals elements of the given matrix 0. Both the original and new matrix will be displayed.

In this program one can take values of the matrix from the user at run time and make the diagonal elements appear 0. Suppose

Original Matrix

1 2 3

4 5 6

7 8 9

New Matrix

0 2 3

4 0 6

7 8 0

Private Sub MatrixDiagonal()

'Create matrix M1.

Dim strArray As String = ""

Dim M1(3, 3) As Integer

Dim i, j As Integer

'i represents row and

'j represents column of all the matrices.

'The i in the for loop considers

'all the rows and j considers all

'the columns.So row is considered first

'and then the column, while performing

'every operation over matrix.

'Add data in matrix M1 and take value from user at runtime.

For i = 0 To 2

For j = 0 To 2

Console.Write("Enter any value")

M1(i, j) = Console.ReadLine

Next

Next

'Display the matrix M1.

For i = 0 To 2

For j = 0 To 2

If strArray.Length = 0 Then

strArray = " " & M1(i, j)

Else

strArray = strArray & " " & M1(i, j)

End If

Next

strArray = strArray & vbNewLine

Next

Console.Write("The original matrix is " & vbNewLine & strArray)

'Making diagonal elements 0.

For i = 0 To 2

For j = 0 To 2

If i = j Then

'If the row no. and column no. are equal

'then save the sum in the variable intSum.

M1(i, j) = 0

End If

Next

Next

'Display the matrix M1 with 0.

strArray = ""

For i = 0 To 2

For j = 0 To 2

If strArray.Length = 0 Then

strArray = " " & M1(i, j)

Else

strArray = strArray & " " & M1(i, j)

End If

Next

strArray = strArray & vbNewLine

Next

Console.Write("The new matrix is " & vbNewLine & strArray)

End Sub