Tuesday, July 28, 2026

Printing a Hollow Rectangle | Simple Shape Patterns in C

Moving from solid shapes to hollow patterns is a significant milestone in your programming journey. A hollow rectangle pattern is an excellent exercise in using conditional logic within your nested loops, teaching you how to differentiate between the boundaries of a shape and its interior.

What You Will Learn

This tutorial will guide you through creating a C program that prints a hollow rectangle of a specified height and width. You will learn to use if-else conditions to ensure that only the border of the rectangle is printed, while the internal space remains empty.

Prerequisites:

  • Understanding of nested for loops.

  • Familiarity with conditional (if-else) statements.

Final Output:

For a rectangle with a height of 4 and a width of 6, the output will look like this:

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

Deconstructing the Pattern: The Logic

1. The Visual Representation

To create a hollow effect, we distinguish between two regions of the rectangle:

  • Border: The top row, bottom row, left column, and right column.

  • Interior: The space inside these boundaries.

2. Problem Statement

The goal is to print a symbol only when the current position $(i, j)$ lies on the edge of the rectangle. Otherwise, we print an empty space.

3. Pattern Analysis & Logic

  • Outer Loop (Rows): Iterates from $i = 1$ to $height$.

  • Inner Loop (Columns): Iterates from $j = 1$ to $width$.

  • Printing Condition: We print the symbol * if:

    • It is the first row ($i == 1$) or the last row ($i == height$).

    • It is the first column ($j == 1$) or the last column ($j == width$).

    • Otherwise, we print a space.

The Code Implementation

#include <stdio.h>

int main() {
    int height = 4; // Vertical height
    int width = 6;  // Horizontal width

    // Outer loop for rows
    for (int i = 1; i <= height; i++) {
        
        // Inner loop for columns
        for (int j = 1; j <= width; j++) {
            // Condition for the border
            if (i == 1 || i == height || j == 1 || j == width) {
                printf("*");
            } else {
                printf(" "); // Space for the hollow interior
            }
        }
        
        // Move to the next line after each row
        printf("\n");
    }

    return 0;
}

Explanation:

  • Boundary Condition: The if statement (i == 1 || i == height || j == 1 || j == width) is the core of this pattern. It targets every coordinate that constitutes the outer shell of the rectangle.

  • else Statement: This effectively leaves the interior empty by printing a space character instead of a *.

  • printf("\n"): This ensures the inner loop concludes its work for one row before moving to the next.

Sample Output and Analysis

User Input:

  • Height = 4, Width = 6.

Program Output:


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

Output Analysis: The loop checks every coordinate. When the row is 1 or 4, or the column is 1 or 6, it prints a star. In all other cases—such as row 2, column 3—it prints a space, creating the desired hollow look.

Common Mistakes and Troubleshooting

  • Logical OR (||): Beginners often confuse || (OR) with && (AND). If you use AND, the pattern will fail because a single position cannot be both the first and the last row simultaneously.

  • Space Printing: Omitting the space inside the else block will collapse the shape, resulting in a distorted output.

Complexity Analysis

  • Time Complexity: O(height \times width), as we visit every cell in the potential grid.

  • Space Complexity: O(1), as the logic does not require additional storage that scales with input.

Conclusion

By leveraging simple conditional statements, you have successfully transformed a solid shape into a hollow one. This is a foundational technique that you will frequently use when designing more complex, artistic, or algorithmic patterns in C.



For all Pattern Programs list click here

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

No comments:

Post a Comment