Sunday, August 23, 2026

Printing Solid Diamond in C | Advanced Shape Patterns

The solid diamond pattern combines two fundamental symmetric structures: an upper full pyramid and a lower inverted full pyramid. Mastering this shape sharpens your ability to design multi-phase loop structures, manage symmetrical alignment, and sequence mathematical boundaries.

Introduction

Building a solid diamond pattern requires splitting the shape into two distinct halves: a growing upper section and a shrinking lower section. This tutorial breaks down the mathematical relationships between row indexes, space padding, and asterisk counts, providing a complete C source implementation and execution breakdown.

  • Prerequisites: Knowledge of nested for loops, standard input/output functions (printf, scanf), and basic conditional statements.

  • Expected Output:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Deconstructing the Pattern Logic

For an upper-half height of $n$, the total height of the diamond is 2n - 1 rows. The program executes in two sequential loop phases:

Phase 1: Upper Pyramid (i = 1 to n)

  • Leading Spaces: Decreases each row ($n - i$).

  • Asterisks: Increases in odd increments ($2i - 1$).

Phase 2: Lower Inverted Pyramid (i = 1 to n - 1)

  • Leading Spaces: Increases each row (i).

  • Asterisks: Decreases in odd increments (2(n - i) - 1).

Row IndexPhaseLeading SpacesAsterisks
1Upper41
2Upper33
3Upper25
4Upper17
5 (Center)Upper09
1 (Row 6)Lower17
2 (Row 7)Lower25
3 (Row 8)Lower33
4 (Row 9)Lower41

Code Implementation


#include <stdio.h>

int main() {
    int n, i, j, space;

    printf("Enter the number of rows for upper half: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    // Phase 1: Upper Full Pyramid (Rows 1 to n)
    for (i = 1; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    // Phase 2: Lower Inverted Pyramid (Rows 1 to n - 1)
    for (i = 1; i <= n - 1; i++) {
        // Print leading spaces
        for (space = 1; space <= i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * (n - i) - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Upper Loop Phase: Handles rows 1 through n, expanding asterisks to construct the widest middle line.

  • Lower Loop Phase: Runs n - 1 times to avoid repeating the maximum width row, contracting asterisks down to a single vertex.

  • Row Formatting: printf("\n"); breaks lines after each iteration of the outer loops.

Compiling and Execution

Compile and execute using standard GCC tooling:

Console Output:

Enter the number of rows for upper half: 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Common Mistakes & Troubleshooting

  • Duplicate Center Line: Iterating the lower outer loop up to n instead of n - 1 duplicates the widest row (9 asterisks for n = 5).

  • Misaligned Offsets: Using space < n - i instead of space <= n - i shifts the entire top half leftward relative to the bottom half.

Complexity Analysis

  • Time Complexity: O(n^2) due to consecutive nested loop blocks iterating proportionally to n.

  • Space Complexity: O(1) auxiliary space requiring only scalar variables.

Conclusion

Modularizing patterns into multi-phase nested loops provides the blueprint for rendering complex geometric outputs in terminal applications.


For all Pattern Programs list click here

…till the next post, bye-bye & take care

No comments:

Post a Comment