JulianJulianov

04.MethodsLab-Printing Triangle

Feb 12th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. 4. Printing Triangle
  2. Create a method for printing triangles as shown below:
  3. Examples
  4. Input   Output
  5. 3       1
  6.         1 2
  7.         1 2 3
  8.         1 2
  9.         1
  10. 4       1
  11.         1 2
  12.         1 2 3
  13.         1 2 3 4
  14.         1 2 3
  15.         1 2
  16.         1
  17.  
  18. using System;
  19.  
  20. namespace _04MethodsLabPrintingTriangle
  21. {
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             int input = int.Parse(Console.ReadLine());
  27.             TopTriangle(input);
  28.             BottomTriangle(input - 1);
  29.             /*
  30.              for (int i = 1; i <= input; i++)
  31.                  PrintLine(1, i); // Print 1... 1 2 ... 1 2 3 n
  32.  
  33.              for (int a = input - 1; a >= 1; a--)
  34.                  PrintLine(1, a); // Print 1 2 3 n ... 1 2 ... 1*/
  35.         }
  36.            
  37.         /*private static void PrintLine(int start, int end)
  38.           {
  39.               for (int i = start; i <= end; i++)
  40.               {
  41.                   Console.Write(i + " ");
  42.               }
  43.               Console.WriteLine();
  44.           }*/
  45.         private static void TopTriangle(int input)
  46.         {
  47.             for (int i = 1; i <= input; i++)
  48.             {
  49.                 for (int a = 1; a <= i; a++)
  50.                 {
  51.                     Console.Write(a + " ");
  52.                 }
  53.                 Console.WriteLine();
  54.             }
  55.         }
  56.         private static void BottomTriangle(int input)
  57.         {
  58.             for (int i = input; i > 0; i--)
  59.             {
  60.                 for (int j = 1; j <= i; j++)
  61.                 {
  62.                     Console.Write(j + " ");
  63.                 }
  64.                 Console.WriteLine();
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment