Monday, July 27, 2026

Printing a Solid Rectangle | Simple Shape Patterns in C

Building upon the foundation of square patterns, the solid rectangle is an essential exercise in learning how to manage independent dimensions. Unlike a square, a rectangle allows us to define distinct values for height and width, providing a clearer understanding of how outer and inner loops control different axes of a grid.

What You Will Learn

This tutorial will guide you through creating a C program that prints a solid rectangle of a specified height and width. By the end of this post, you will understand how to pass two separate variables to your nested loops to control the output shape.

Prerequisites:

  • Understanding of for loops.

  • Familiarity with the printf function.

Final Output:

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


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

Deconstructing the Pattern: The Logic

1. The Visual Representation

A rectangle is defined by two dimensions:

  • Rows (Height): The vertical dimension.

  • Columns (Width): The horizontal dimension.

2. Problem Statement

The goal is to print a pattern of symbols where the number of rows (r) and the number of columns (c) can be specified independently.

3. Pattern Analysis & Logic

  • Outer Loop (Rows): Controls the vertical count. It runs from i = 1 to r.

  • Inner Loop (Columns): Controls the horizontal count. It runs from j = 1 to c.

  • Algorithm:

    1. Define the height (r) and width (c).

    2. Use an outer loop to iterate through each row.

    3. Use an inner loop to print the symbol c times.

    4. Print a newline character after each row to maintain the rectangular structure.

The Code Implementation


#include <stdio.h>

int main() {
    int rows = 3; // Vertical height
    int cols = 5; // Horizontal width

    // Outer loop for the number of rows
    for (int i = 1; i <= rows; i++) {
        
        // Inner loop for the number of columns
        for (int j = 1; j <= cols; j++) {
            printf("*"); // Print the symbol
        }
        
        // Move to the next line after each row
        printf("\n"); 
    }

    return 0;
}

Explanation:

  • int rows = 3; and int cols = 5;: These variables define the geometry of your rectangle.

  • for (int i = 1; i <= rows; i++): The outer loop ensures the pattern repeats for the specified height.

  • for (int j = 1; j <= cols; j++): The inner loop determines the width by printing the symbol cols times per row.

  • printf("\n");: This ensures that the column loop restarts on a fresh line, completing the rectangular shape.

Sample Output and Analysis

User Input:

  • Height = 3, Width = 5.

Program Output:


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

Output Analysis: The outer loop executes 3 times, and for every iteration, the inner loop executes 5 times, resulting in a perfectly aligned 3-row, 5-column rectangle.

Complexity Analysis

  • Time Complexity: O(r \times c), where r is the number of rows and c is the number of columns.

  • Space Complexity: O(1), as the memory usage remains constant.

Conclusion

By decoupling the row and column logic, you have successfully moved from a constrained square to a flexible rectangular grid. This flexibility is a key skill for designing more complex visual patterns in C.



For all Pattern Programs list click here

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

No comments:

Post a Comment