0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Diamond Pattern

You can print a diamond pattern in C# by using nested 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

Input5
Output * *** ***** ******* ********* ******* ***** *** *
Input3
Output * *** ***** *** *
Input1
Output*
🧠

How to Think About It

To print a diamond pattern, think of it as two parts: the top half which grows from 1 star to the maximum odd number of stars, and the bottom half which shrinks back down. For each line, print spaces first to center the stars, then print the stars. Use for loops to control the number of spaces and stars on each line.
📐

Algorithm

1
Get the number of rows (n) for the diamond's half height.
2
For each line from 1 to n, print spaces (n - line) and then stars (2 * line - 1).
3
For each line from n-1 down to 1, print spaces (n - line) and then stars (2 * line - 1).
4
Print a new line after each row.
💻

Code

csharp
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));
        }
    }
}
Output
* *** ***** ******* ********* ******* ***** *** *
🔍

Dry Run

Let's trace the diamond pattern printing for n = 3 through the code.

1

Top half line 1

Print 2 spaces and 1 star: ' *'

2

Top half line 2

Print 1 space and 3 stars: ' ***'

3

Top half line 3

Print 0 spaces and 5 stars: '*****'

4

Bottom half line 1

Print 1 space and 3 stars: ' ***'

5

Bottom half line 2

Print 2 spaces and 1 star: ' *'

LineSpacesStarsOutput
121 *
213 ***
305*****
413 ***
521 *
💡

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 single loop with conditional
csharp
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));
        }
    }
}
This uses one loop and calculates stars and spaces based on line number, reducing code duplication but slightly harder to read.
Using recursion
csharp
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);
    }
}
Recursion prints the top half going down and bottom half coming back up, elegant but less intuitive for beginners.

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.

ApproachTimeSpaceBest For
Two separate loopsO(n^2)O(1)Clarity and simplicity
Single loop with conditionO(n^2)O(1)Less code duplication
RecursionO(n^2)O(n) due to call stackElegant but uses more stack memory
💡
Use new string(' ', count) and new string('*', count) to easily print repeated characters.
⚠️
Beginners often forget to print the bottom half of the diamond or miscalculate spaces, causing misaligned patterns.