Advertisement
dimipan80

Advanced Topics 7. Matrix of Palindromes

Jul 3rd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. // Write a program to generate the following matrix of palindromes of 3 letters with r rows and c columns.
  2.  
  3. namespace _07.MatrixOfPalindromes
  4. {
  5.     using System;
  6.  
  7.     public class MatrixOfPalindromes
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 int countR;
  14.                 do
  15.                 {
  16.                     Console.Write("Enter Count of rows R: ");
  17.                 }
  18.                 while (!int.TryParse(Console.ReadLine(), out countR) || countR < 1);
  19.  
  20.                 int countC;
  21.                 do
  22.                 {
  23.                     Console.Write("Enter Count of columns C: ");
  24.                 }
  25.                 while (!int.TryParse(Console.ReadLine(), out countC) || countC < 1);
  26.  
  27.                 char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  28.  
  29.                 Console.WriteLine("The Matrix of Palindromes is:\n");
  30.                 for (int rows = 0; rows < countR; rows++)
  31.                 {
  32.                     for (int cols = rows; cols < rows + countC; cols++)
  33.                     {
  34.                         string palindrome = alphabet[rows % alphabet.Length].ToString() + alphabet[cols % alphabet.Length].ToString() + alphabet[rows % alphabet.Length].ToString();  
  35.                         Console.Write("{0,4}", palindrome);
  36.                     }
  37.  
  38.                     Console.WriteLine();
  39.                 }
  40.  
  41.                 Console.WriteLine();
  42.             }
  43.         }        
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement