C# C# Console

Print a Matrix of Size n*n in Spiral Order Using C#

In this example,i’ll show you How to print a matrix of size n*n in spiral order using C#.

To rotate a matrix in spiral order, we need to do following until all the inner matrix and the outer matrix are covered −

  • Step1 − Move elements of top row
  • Step2 − Move elements of last column
  • Step3 − Move elements of bottom row
  • Step4 − Move elements of first column
  • Step5 − Repeat above steps for inner ring while there is an inner matrix

C# Code:

Output:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.