If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# program to print a given matrix in spiral form.
To increase your C# knowledge, practice all C sharp programs:
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; class matrixSpiralFormProgram { static void spiralPrint(int m, int n, int [,]a) { int i, k = 0, l = 0; while (k < m && l < n) { for (i = l; i < n; ++i) { Console.Write(a[k, i] +" "); } k++; for (i = k; i < m; ++i) { Console.Write(a[i,n - 1]+" "); } n--; if ( k < m) { for (i = n-1; i >= l; --i) { Console.Write(a[m - 1, i]+" "); } m--; } if (l < n) { for (i = m-1; i >= k; --i) { Console.Write(a[i, l] + " "); } l++; } } } public static void Main () { int R = 3; int C = 6; int [,]a = { {1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18} }; spiralPrint(R,C,a); } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.