The solid rhombus pattern is a fundamental logic-building program in C pattern programming that bridges basic square printing with skewed space alignment
Introduction
Mastering the solid rhombus pattern sharpens your control over nested loops by combining a dynamically decreasing space offset with a constant count of printed symbols per row
Prerequisites: Knowledge of nested
forloops, standard input/output functions (printf,scanf), and basic integer operations. Expected Output:
*****
*****
*****
*****
Deconstructing the Pattern Logic
A solid rhombus of size n consists of n rows and n columns of asterisks, shifted horizontally by leading spaces to form a sloped parallelogram with equal sides
Outer Loop: Controls the row index i, iterating from 1 to n.
Space Loop: Prints n - i leading spaces on row i to generate the rightward slant
. Asterisk Loop: Prints exactly n asterisks on every row to preserve constant width
.
| Row (i) | Leading Spaces (n−i) | Asterisks (n) |
| 1 | 4 | 5 |
| 2 | 3 | 5 |
| 3 | 2 | 5 |
| 4 | 1 | 5 |
| 5 | 0 | 5 |
Code Implementation
int main() {
int n, i, j, space;
printf("Enter the number of rows: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= n; i++) {
// Print leading spaces for slant alignment
for (space = 1; space <= n - i; space++) {
printf(" ");
}
// Print constant width of asterisks
for (j = 1; j <= n; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Code Breakdown
Space Offset: The loop bound
space <= n - idecreases the space count progressively, creating the left-to-right slant. Fixed Width Loop: Unlike pyramid structures where asterisk counts vary per row, the inner character loop iterates up to $n$ consistently on every row
. Row Formatting:
printf("\n");breaks the output line cleanly after completing the asterisk loop for each row.
Compiling and Execution
Compile and run the program using standard GCC tools
Console Output:
*****
*****
*****
*****
*****
Common Mistakes & Troubleshooting
Confusing Rhombus with Pyramids: Dynamically changing the asterisk count per row instead of keeping it constant at n converts the shape into a triangular pattern
. Inverted Slant Direction: Using i - 1 leading spaces instead of n - i slants the rhombus in the opposite direction
. Missing Line Breaks: Omitting
printf("\n");prints all symbols across a single horizontal line.
Complexity Analysis
Time Complexity: O(n^2) because the outer loop runs n times containing two sequential inner loops that iterate up to n times
. Space Complexity: O(1) auxiliary memory space, using only scalar integer variables
.
Conclusion
Combining constant character widths with dynamic space offsets provides a foundation for rendering skewed geometric grids in lower-level programming
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment