C# Program to Print Pyramid Pattern
Use nested
for loops in C# where the outer loop controls the rows and the inner loops print spaces and stars; for example, for (int i = 1; i <= n; i++) { for (int j = n - i; j > 0; j--) Console.Write(" "); for (int k = 0; k < 2 * i - 1; k++) Console.Write("*"); Console.WriteLine(); } prints a pyramid of height n.Examples
Input3
Output *
***
*****
Input5
Output *
***
*****
*******
*********
Input1
Output*
How to Think About It
To print a pyramid pattern, think of each row as having some spaces followed by stars. The number of spaces decreases as you go down, and the number of stars increases. Use one loop to go through each row, then inside it, print spaces first, then stars.
Algorithm
1
Get the number of rows (height) from the user.2
For each row from 1 to height:3
Print (height - current row) spaces.4
Print (2 * current row - 1) stars.5
Move to the next line.6
Repeat until all rows are printed.Code
csharp
using System; class Program { static void Main() { int n = 5; for (int i = 1; i <= n; i++) { for (int j = n - i; j > 0; j--) Console.Write(" "); for (int k = 0; k < 2 * i - 1; k++) Console.Write("*"); Console.WriteLine(); } } }
Output
*
***
*****
*******
*********
Dry Run
Let's trace the pyramid printing for n=3 through the code.
1
Row 1
Print 2 spaces and 1 star: " *"
2
Row 2
Print 1 space and 3 stars: " ***"
3
Row 3
Print 0 spaces and 5 stars: "*****"
| Row | Spaces | Stars | Output |
|---|---|---|---|
| 1 | 2 | 1 | * |
| 2 | 1 | 3 | *** |
| 3 | 0 | 5 | ***** |
Why This Works
Step 1: Outer loop controls rows
The outer for loop runs from 1 to n, each iteration prints one row of the pyramid.
Step 2: Print spaces first
For each row, print n - i spaces to align stars in the center.
Step 3: Print stars next
Print 2 * i - 1 stars to form the pyramid shape.
Alternative Approaches
Using while loops
csharp
using System; class Program { static void Main() { int n = 5, i = 1; while (i <= n) { int j = n - i; while (j > 0) { Console.Write(" "); j--; } int k = 0; while (k < 2 * i - 1) { Console.Write("*"); k++; } Console.WriteLine(); i++; } } }
This uses while loops instead of for loops; functionally the same but slightly longer.
Using string.PadLeft and string.PadRight
csharp
using System; class Program { static void Main() { int n = 5; for (int i = 1; i <= n; i++) { string stars = new string('*', 2 * i - 1); Console.WriteLine(stars.PadLeft(n + i - 1)); } } }
This uses built-in string padding methods to simplify space printing.
Complexity: O(n^2) time, O(1) space
Time Complexity
The program uses nested loops: the outer loop runs n times and the inner loops run up to n times, resulting in O(n^2) time.
Space Complexity
Only a few variables are used; no extra space grows with input size, so space complexity is O(1).
Which Approach is Fastest?
All approaches have similar time complexity; using string padding may be more readable but not faster.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loops | O(n^2) | O(1) | Clear control of spaces and stars |
| While loops | O(n^2) | O(1) | Alternative syntax, same performance |
| String padding | O(n^2) | O(1) | Simpler code, less manual space printing |
Use nested loops: one for spaces and one for stars to easily control pyramid shape.
Beginners often forget to print the correct number of spaces, causing the pyramid to be misaligned.