Last active
July 15, 2021 07:20
-
-
Save schauhan232/7ec26d12d56528ebde7353d688255905 to your computer and use it in GitHub Desktop.
C# Print Diagonal matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var matrix = new int[,] { | |
{ 1, 2, 3, 4,5}, | |
{ 6, 7, 8, 9, 10}, | |
{ 11, 12, 13, 14, 15}, | |
{ 16, 17, 18, 19, 20} | |
}; | |
var rows = matrix.GetLength(0); | |
var columns = matrix.GetLength(1); | |
var rowPointer = 0; | |
Console.WriteLine("First half of the diagonal"); | |
//start from first row to last row | |
while (rowPointer < rows) | |
{ | |
var i = rowPointer; | |
var j = 0; | |
//rows to column | |
while (i >= 0) | |
{ | |
Console.Write(matrix[i, j]); | |
i--; | |
j++; | |
} | |
rowPointer++; | |
Console.WriteLine(); | |
} | |
var columnPointer = 1; | |
Console.WriteLine("Second half of the diagonal"); | |
// number 1 column to all column | |
while (columnPointer < columns) | |
{ | |
var i = rows - 1; | |
var j = columnPointer; | |
//til last column and last row | |
while (j <= columns - 1) | |
{ | |
Console.Write(matrix[i, j]); | |
i--; | |
j++; | |
} | |
columnPointer++; | |
Console.WriteLine(); | |
} | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment