# VB.Net program to print the right diagonal of Matrix

Here, we are going to learn how to print the right diagonal of Matrix in VB.Net?
Submitted by Nidhi, on December 12, 2020

Here, we will read a 3X3 matrix from the user then print the right diagonal of MATRIX on the console screen.

Program/Source Code:

The source code to print the right diagonal of Matrix is given below. The given program is compiled and executed successfully.

```'VB.Net program to print the right diagonal of the MATRIX.

Module Module1

Sub Main()
Dim arr(,) As Integer = New Integer(3, 3) {}

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

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

Console.WriteLine("Right Diagonal of Matrix: ")
For i = 0 To 2 Step 1
For j = 0 To 2 Step 1
If (i + j = 2) Then
Console.Write("{0} ", arr(i, j))
Else
Console.Write("  ")
End If
Next
Console.WriteLine()
Next
End Sub

End Module
```

Output:

```Enter Matrix elements:
Enter element[0][0]: 10
Enter element[0][1]: 20
Enter element[0][2]: 30
Enter element[1][0]: 40
Enter element[1][1]: 50
Enter element[1][2]: 60
Enter element[2][0]: 70
Enter element[2][1]: 80
Enter element[2][2]: 90
Matrix:
10 20 30
40 50 60
70 80 90
Right Diagonal of Matrix:
30
50
70
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: ")
For i = 0 To 2 Step 1
For j = 0 To 2 Step 1
Console.Write("{0} ", arr(i, j))
Next
Console.WriteLine()
Next

Console.WriteLine("Right Diagonal of Matrix: ")
For i = 0 To 2 Step 1
For j = 0 To 2 Step 1
If (i + j = 2) Then
Console.Write("{0} ", arr(i, j))
Else
Console.Write("  ")
End If
Next
Console.WriteLine()
Next
```

In the above code, we printed the MATRIX and right diagonal MATRIX on the console screen.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates