C# PROGRAM TO PRINT MATRIX IN A SNAIL SHELL WAY
/**C# PROGRAM TO PRINT MATRIX IN SNAIL SHELL FORMAT**///for printing matrix as in the below given format// 1 2 3 4// 12 13 14 5// 11 16 15 6// 10 9 8 7
using System; namespace SnailMatrix { class Program { static void Main(string[] args) { //int rows, cols, int size; int a = 0;// b = 1; int i, j, k, count = 0, flag = 0; do { try { Console.WriteLine("\n>>>PROGRAM To PRINT A MULTI-DIMENSIONAL ARRAY walking IN A SNAIL SHELL WAY <<<"); Console.Write("\n Enter the Size of a square Matrix(between 2 to 10):"); size = Convert.ToInt16(Console.ReadLine()); if (size<2||size > 10) //limiting the size of matrix { Console.BackgroundColor = ConsoleColor.DarkRed; //changing background color to red Console.WriteLine("The Size Of Matrix should Be in between 2 and 10."); //System.Console.ResetColor(); ///resetting color Console.ReadKey(); return; } else { flag = 1; int[,] matrix = new int[size, size]; for (k = 1; k < size; k++, a++) { for (i = a, j = a; j < size - k; j++) //lef { matrix[i, j] = ++count; } for (i = a, j = size - k; i < size - k; i++) { matrix[i, j] = ++count; } for (i = size - k, j = size - k; j >= a; j--) { matrix[i, j] = ++count; } for (i = size - (k + 1), j = a; i >= k; i--) { matrix[i, j] = ++count; } } // Console.Write("\n\n\t>>> " + size + " * " + size + " MATRIX <<<\n\t---------------------------------------\n\t"); Console.Write("\n\n\t"); for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (matrix[i, j] < 10) { Console.Write(" 0" + Convert.ToString(matrix[i, j])); } else { Console.Write(" " + Convert.ToString(matrix[i, j])); } if (j == size - 1) { Console.Write("\n\t"); } } } } } catch //to catch exceptions,suppose string entered as a size of matrix { Console.BackgroundColor = ConsoleColor.DarkRed; Console.WriteLine("WARNING:only Number between (2 and 10) is allowed"); Console.ResetColor(); } Console.WriteLine("\n\n\t Press any key to exit...."); }while(flag==0); Console.ReadKey(); } }
}
For c program of this Click Here .
OUTPUTS:
(output:1)
(output : 2) if we are giving size greater than 10 or less than 2
(output : 3) if we are entering input other than int type..
More Posts:
Nice post very helpful
ReplyDeletedbakings
nice post thanks
ReplyDeleteappvn
Thanks for sharing such a helpful, and understandable blog. I really enjoyed reading it.
ReplyDeleteRobots for kids
Robotic Online Classes
Robotics School Projects
Programming Courses Malaysia
Coding courses
Coding Academy
coding robots for kids
Coding classes for kids
Coding For Kids
helpful article for beginners..
ReplyDeleteC Arrays examples
Loops in C examples
if-else in C with examples
Strings in C with examples