Introduction
Printing pattern programs is a common programming exercise for understanding loops and conditional logic in C#. A pyramid pattern is a good example because it requires controlling both spaces and asterisks based on the current row.
In this example, we will print a centered pyramid using * characters. The value 7 represents the number of rows.
The expected output is:
*
***
*****
*******
*********
***********
*************
C# Pyramid Pattern Program
The following program uses nested for loops to print the pyramid.
using System;
public class Program
{
public static void Main()
{
PrintPyramid(7);
}
static void PrintPyramid(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= (n * 2 - 1); j++)
{
if (j <= n - i || j >= n + i)
Console.Write(" ");
else
Console.Write("*");
}
Console.WriteLine();
}
}
}
How the Logic Works
The method accepts the number of rows through the n parameter:
PrintPyramid(7);
Therefore, the program creates a pyramid with seven rows.
The outer loop controls the rows:
for (int i = 1; i <= n; i++)
For n = 7, the loop executes seven times.
The inner loop controls the positions in each row:
for (int j = 1; j <= (n * 2 - 1); j++)
A pyramid with n rows requires a maximum width of:
2 × n - 1
For seven rows:
2 × 7 - 1 = 13
So every row is evaluated across 13 character positions.
Understanding the Condition
The most important part of the program is:
if (j <= n - i || j >= n + i)
Console.Write(" ");
else
Console.Write("*");
The condition determines whether the current position should contain a space or an asterisk.
For the first row:
i = 1
n = 7
The star positions are:
7
For the second row:
i = 2
The star positions are:
6 7 8
For the third row:
i = 3
The star positions are:
5 6 7 8 9
The number of stars increases by two for every new row.
Dry Run
For n = 7, the program produces the following pattern:
Row | Spaces Before | Stars | Total Width |
|---|---|---|---|
1 | 6 | 1 | 13 |
2 | 5 | 3 | 13 |
3 | 4 | 5 | 13 |
4 | 3 | 7 | 13 |
5 | 2 | 9 | 13 |
6 | 1 | 11 | 13 |
7 | 0 | 13 | 13 |
The general formula for the number of stars in row i is:
2 × i - 1
The number of leading spaces is:
n - i
This is what keeps the pyramid centered.
Output
Running the program with:
PrintPyramid(7);
produces:
*
***
*****
*******
*********
***********
*************
Why Use n * 2 - 1 Positions?
The widest row contains 2n - 1 stars.
For example, with seven rows:
2 × 7 - 1 = 13
The final row therefore contains 13 asterisks:
*************
Using the same width for every row allows the program to position the stars correctly in the center.
Time and Space Complexity
The outer loop runs n times, and the inner loop runs approximately 2n - 1 times for every row.
Therefore, the time complexity is:
O(n²)
The program does not use additional data structures proportional to the input size, so its auxiliary space complexity is:
O(1)
The output itself naturally requires O(n²) characters because the program prints approximately n² positions.
A More Direct Way to Think About the Pattern
The same pyramid can be understood using two simple formulas:
Leading spaces = n - i
Stars = 2 × i - 1
For n = 7:
Row 1 → 6 spaces + 1 star
Row 2 → 5 spaces + 3 stars
Row 3 → 4 spaces + 5 stars
Row 4 → 3 spaces + 7 stars
Row 5 → 2 spaces + 9 stars
Row 6 → 1 space + 11 stars
Row 7 → 0 spaces + 13 stars
This approach makes the pattern easier to understand and also provides a foundation for solving other programming-pattern problems.
Conclusion
The pyramid pattern is a simple but useful C# programming exercise for practicing nested loops and conditional statements.
The key idea is to divide every row into character positions and determine whether each position should contain a space or an asterisk. The pyramid contains 2 × i - 1 stars on row i, while the number of leading spaces is n - i.
For n = 7, the result is a centered pyramid with seven rows.
Understanding these formulas makes it easier to solve more advanced pattern-printing problems involving pyramids, diamonds, triangles, and other geometric shapes.
Join the conversation! Your thoughts help the community grow.