Advertisement
dimipan80

2. Drawing_Isosceles_Triangle

Jun 1st, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. /* Write a program that prints an isosceles triangle of 9 copyright symbols Β©.
  2.  * Note that the Β© symbol may be displayed incorrectly at the console so you may need
  3.  * to change the console character encoding to UTF-8 and assign a Unicode-friendly font
  4.  * in the console. Note also, that under old versions of Windows the Β© symbol
  5.  * may still be displayed incorrectly, regardless of how much effort you put to fix it. */
  6.  
  7. namespace _8.IsoscelesTriangle
  8. {
  9.     using System;
  10.     using System.Text;
  11.  
  12.     public class DrawIsoscelesTriangle
  13.     {
  14.         public static void Main(string[] args)
  15.         {
  16.             Console.OutputEncoding = Encoding.UTF8;
  17.             char copySymb = '\u00A9';
  18.             int countRows = 4;                        
  19.             Console.WriteLine("The Isosceles Triangle with 9 copyright symbols '{0}' is:", copySymb);
  20.             Console.WriteLine();
  21.  
  22.             // Print the first top row of Triangle:
  23.             string spaceBeforeSymbol = new string(' ', countRows - 1);
  24.             Console.WriteLine(spaceBeforeSymbol + copySymb);
  25.  
  26.             // Print next rows below:
  27.             for (int row = 1; row < countRows - 1; row++)
  28.             {
  29.                 PrintNextRowOfTriangle(countRows, row, copySymb);
  30.             }
  31.  
  32.             // Print the last bottom row of Triangle:
  33.             for (int i = 0; i < countRows; i++)
  34.             {
  35.                 Console.Write(copySymb + " ");
  36.             }
  37.  
  38.             Console.WriteLine();
  39.             Console.WriteLine();
  40.         }
  41.  
  42.         private static void PrintNextRowOfTriangle(int countRows, int row, char symbol)
  43.         {
  44.             // Calculate lenght of whole current row:
  45.             int lenght = countRows + row;
  46.  
  47.             // Calculate whitespace before First copyright symbol:
  48.             int countSpacesBeforeFirstSymbol = (countRows - 1) - row;
  49.             string spaceBeforeFirstSymbol = new string(' ', countSpacesBeforeFirstSymbol);
  50.  
  51.             // Calculate whitespace between left and right copyright symbols on the row:
  52.             int countMiddleSpaces = (lenght - countSpacesBeforeFirstSymbol) - 2;
  53.             string middleSpace = new string(' ', countMiddleSpaces);
  54.  
  55.             // Print whole current row:
  56.             Console.WriteLine(spaceBeforeFirstSymbol + symbol + middleSpace + symbol);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement