Triangle Patterns in C#

Hello geeks!

This article shows how to draw patterns in a C# console program, especially triangle patterns, like upward and downward.

Triangle: 1

This triangle is a simple triangle; we can draw that in several ways. I am showing two techniques of doing this using a simple for loop.

The first one will be like this:

  1. for (j = 1; j <= i; j++)  
  2. {  
  3.    Console.Write("");  
  4. }  
  5. for (k = 1; k <= i; k++)  
And the second will follow this:
  1. for (j = 1; j <= val-i; j++)  
  2. {  
  3.    // Console.Write("");  
  4. }  
  5. for (k = 1; k <= i; k++)  
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.    class Program  
  8.    {  
  9.       static void Main(string[] args)  
  10.       {  
  11.          int val = 5;  
  12.          int i, j, k ;  
  13.          for (i = 1; i <= val; i++)  
  14.          {  
  15.             for (j = 1; j <= i; j++)  
  16.             {  
  17.                Console.Write("");  
  18.             }  
  19.             for (k = 1; k <= i; k++)  
  20.             {  
  21.                Console.Write("*");  
  22.             }  
  23.             Console.WriteLine("");  
  24.          }  
  25.          Console.ReadLine();  
  26.       }  
  27.    }  
  28. }  
Or you can write it as in the following by making some minor changes:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.    class Program  
  8.    {  
  9.       static void Main(string[] args)  
  10.       {  
  11.          int val = 5;  
  12.          int i, j, k ;  
  13.          for (i = 1; i <= val; i++)  
  14.          {  
  15.             for (j = 1; j <= val-i; j++)  
  16.             {  
  17.                // Console.Write("");  
  18.             }  
  19.             for (k = 1; k <= i; k++)  
  20.             {  
  21.                Console.Write("*");  
  22.             }  
  23.             Console.WriteLine("");  
  24.          }  
  25.          Console.ReadLine();  
  26.       }  
  27.    }  
  28. }  
Output Window



Triangle: 2

The triangle in this category is just the opposite of the first type of triangle patterns. There is no difficulty in applying loops for them, what we need to do is to make some little changes in the for loop conditions and it will work fine.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.    class Program  
  8.    {  
  9.       static void Main(string[] args)  
  10.       {  
  11.          int val = 5;  
  12.          int i, j, k ;  
  13.          for (i = 1; i <= val; i++)  
  14.          {  
  15.             for (j = 1; j <= val-i; j++)  
  16.             {  
  17.                Console.Write(" ");  
  18.             }  
  19.             for (k = 1; k <= i; k++)  
  20.             {  
  21.                Console.Write("*");  
  22.             }  
  23.             Console.WriteLine("");  
  24.          }  
  25.          Console.ReadLine();  
  26.       }  
  27.    }  
  28. }  
(In the same manner as I mentioned for the first triangle you can also do following pattern too by applying some minor changes.)

Output Window



Triangle: 3

In this category we just get a mirror triangle of the first triangle pattern. You can also make changes in the second category to get a mirror triangle pattern of that.

But the following is the code for only the first cat of triangle mirror.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.    class Program  
  8.    {  
  9.       static void Main(string[] args)  
  10.       {  
  11.          int val = 5;  
  12.          int i, j, k ;  
  13.          for (i = 1; i <= val; i++)  
  14.          {  
  15.             for (j = 1; j <= val-i; j++)  
  16.             {  
  17.                // Console.Write(" ");  
  18.             }  
  19.             for (k = 1; k <= j; k++)  
  20.             {  
  21.                Console.Write("*");  
  22.             }  
  23.             Console.WriteLine("");  
  24.          }  
  25.          Console.ReadLine();  
  26.       }  
  27.    }  
  28. }  
Output Window



Recommended Free Ebook
Similar Articles