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:
- private static void DiamondOne()
- {
- int i, j, count = 1, number;
- Console.Write("Enter number of rows:");
- number = int.Parse(Console.ReadLine());
- count = number - 1;
- for (j = 1; j <= number; j++)
- {
- for (i = 1; i <= count; i++)
- Console.Write(" ");
- count--;
- for (i = 1; i <= 2 * j - 1; i++)
- Console.Write("*");
- Console.WriteLine();
- }
- count = 1;
- for (j = 1; j <= number - 1; j++)
- {
- for (i = 1; i <= count; i++)
- Console.Write(" ");
- count++;
- for (i = 1; i <= 2 * (number - j) - 1; i++)
- Console.Write("*");
- Console.WriteLine();
- }
- Console.ReadLine();
- }
O/P

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



Join the conversation! Your thoughts help the community grow.