The solid diamond pattern combines two fundamental symmetric structures: an upper full pyramid and a lower inverted full pyramid
Introduction
Building a solid diamond pattern requires splitting the shape into two distinct halves: a growing upper section and a shrinking lower section
Prerequisites: Knowledge of nested
forloops, 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
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 Index | Phase | Leading Spaces | Asterisks |
| 1 | Upper | 4 | 1 |
| 2 | Upper | 3 | 3 |
| 3 | Upper | 2 | 5 |
| 4 | Upper | 1 | 7 |
| 5 (Center) | Upper | 0 | 9 |
| 1 (Row 6) | Lower | 1 | 7 |
| 2 (Row 7) | Lower | 2 | 5 |
| 3 (Row 8) | Lower | 3 | 3 |
| 4 (Row 9) | Lower | 4 | 1 |
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
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 - iinstead ofspace <= n - ishifts 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