C# Program to Print Diamond Pattern
for loops to print spaces and stars in the correct order, like for (int i = 1; i <= n; i++) { for (int j = n - i; j > 0; j--) Console.Write(" "); for (int k = 1; k <= 2 * i - 1; k++) Console.Write("*"); Console.WriteLine(); } for the top half and a similar loop for the bottom half.Examples
How to Think About It
for loops to control the number of spaces and stars on each line.Algorithm
Code
using System; class Program { static void Main() { int n = 5; for (int i = 1; i <= n; i++) { Console.Write(new string(' ', n - i)); Console.WriteLine(new string('*', 2 * i - 1)); } for (int i = n - 1; i >= 1; i--) { Console.Write(new string(' ', n - i)); Console.WriteLine(new string('*', 2 * i - 1)); } } }
Dry Run
Let's trace the diamond pattern printing for n = 3 through the code.
Top half line 1
Print 2 spaces and 1 star: ' *'
Top half line 2
Print 1 space and 3 stars: ' ***'
Top half line 3
Print 0 spaces and 5 stars: '*****'
Bottom half line 1
Print 1 space and 3 stars: ' ***'
Bottom half line 2
Print 2 spaces and 1 star: ' *'
| Line | Spaces | Stars | Output |
|---|---|---|---|
| 1 | 2 | 1 | * |
| 2 | 1 | 3 | *** |
| 3 | 0 | 5 | ***** |
| 4 | 1 | 3 | *** |
| 5 | 2 | 1 | * |
Why This Works
Step 1: Printing spaces
We print spaces first to center the stars. The number of spaces decreases as we go down the top half and increases in the bottom half.
Step 2: Printing stars
Stars increase by 2 each line in the top half (1, 3, 5, ...) and decrease by 2 in the bottom half to form the diamond shape.
Step 3: Using loops
Two loops handle the top and bottom halves separately, making the pattern symmetrical.
Alternative Approaches
using System; class Program { static void Main() { int n = 5; int totalLines = 2 * n - 1; for (int i = 1; i <= totalLines; i++) { int stars = i <= n ? 2 * i - 1 : 2 * (totalLines - i + 1) - 1; int spaces = (totalLines - stars) / 2; Console.Write(new string(' ', spaces)); Console.WriteLine(new string('*', stars)); } } }
using System; class Program { static void PrintDiamond(int n, int i = 1) { if (i > n) return; Console.Write(new string(' ', n - i)); Console.WriteLine(new string('*', 2 * i - 1)); PrintDiamond(n, i + 1); if (i < n) { Console.Write(new string(' ', n - i)); Console.WriteLine(new string('*', 2 * i - 1)); } } static void Main() { PrintDiamond(5); } }
Complexity: O(n^2) time, O(1) space
Time Complexity
The program uses nested loops where the outer loop runs about 2n times and inner loops print spaces and stars up to 2n characters, resulting in O(n^2) time.
Space Complexity
Only a few variables and temporary strings are used, so space complexity is O(1) constant extra space.
Which Approach is Fastest?
Both the two-loop and single-loop approaches have similar time complexity; the single-loop reduces code duplication but does not improve speed significantly.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Two separate loops | O(n^2) | O(1) | Clarity and simplicity |
| Single loop with condition | O(n^2) | O(1) | Less code duplication |
| Recursion | O(n^2) | O(n) due to call stack | Elegant but uses more stack memory |
new string(' ', count) and new string('*', count) to easily print repeated characters.