Triangle/Diamond Pattern Programming In C#

In almost every interview, the interviewer asks you to write a program to print diamond/ triangle with star/ number.

In almost every interview, the interviewer will ask you to write a program to print diamond/triangle with the stars or numbers.

In this, we will see how to print a triangle and diamond.

Pattern 1 - Diamond shape with the * symbol. (DiamondOne() method)

Diamond shape: we have to use two for loops and under that, two more for loops to print the * and space.

Example:

  1. private static void DiamondOne()  
  2.        {  
  3.            int i, j, count = 1, number;  
  4.            Console.Write("Enter number of rows:");  
  5.            number = int.Parse(Console.ReadLine());  
  6.            count = number - 1;  
  7.            for (j = 1; j <= number; j++)  
  8.            {  
  9.                for (i = 1; i <= count; i++)  
  10.                    Console.Write(" ");  
  11.                count--;  
  12.                for (i = 1; i <= 2 * j - 1; i++)  
  13.                    Console.Write("*");  
  14.                Console.WriteLine();  
  15.            }  
  16.            count = 1;  
  17.            for (j = 1; j <= number - 1; j++)  
  18.            {  
  19.                for (i = 1; i <= count; i++)  
  20.                    Console.Write(" ");  
  21.                count++;  
  22.                for (i = 1; i <= 2 * (number - j) - 1; i++)  
  23.                    Console.Write("*");  
  24.                Console.WriteLine();  
  25.            }  
  26.            Console.ReadLine();  
  27.   }  

O/P

Triangle Diamond Pattern Programming In C#

Pattern 2 - Triangle shape with the * symbol (TriangleTwo() Method).

Triangle shape: if we remove the second outer for loop, then it will print in the triangle shape.

Example:

  1. private static void TriangleTwo()  
  2.  {  
  3.      int number, i, j, count = 1;  
  4.      Console.Write("Enter number of rows:");  
  5.      number = int.Parse(Console.ReadLine());  
  6.      count = number - 1;  
  7.      for (j = 1; j <= number; j++)  
  8.      {  
  9.          #region Printing Space  
  10.          for (i = 1; i <= count; i++)  
  11.              Console.Write(" ");  
  12.          count--;  
  13.          #endregion  
  14.          for (i = 1; i <= 2 * j - 1; i++)  
  15.              Console.Write("*");  
  16.          Console.WriteLine();  
  17.      }  
  18.      Console.ReadLine();  
  19.  }   

O/P

Triangle Diamond Pattern Programming In C#

Pattern 3 - Triangle shape with the * symbol (TriangleOne() Method).

And again, if we remove the first for loop region highlighted in the yellow color, then, the triangle will be printed in the below shape.

Triangle Diamond Pattern Programming In C#

Code 

  1. private static void TriangleOne()  
  2.         {  
  3.             int number, i, j, count = 1;  
  4.             Console.Write("Enter number of rows:");  
  5.             number = int.Parse(Console.ReadLine());  
  6.             count = number - 1;  
  7.             for (j = 1; j <= number; j++)  
  8.             {  
  9.                 for (i = 1; i <= 2 * j - 1; i++)  
  10.                     Console.Write("*");  
  11.                 Console.WriteLine();  
  12.             }  
  13.             Console.ReadLine();  
  14.         }  

In similar fashion, we can print the same by using numbers. Replace  *  with numbers and it will work.