C (programming language)

C program to print hollow square number pattern

Understanding the Pattern

  • Imagine a square frame composed of numbers, with only the outer edges filled.
  • The pattern maintains a consistent shape regardless of its size.
  • It exhibits symmetry and visual appeal.

Key Concepts Involved

  • Loops: Iterate through rows and columns to construct the pattern’s structure.
  • Conditional Statements: Determine whether to print a number or a space based on its position within the square.
  • Number Printing: Use printf to display numbers on the console.

Step-by-Step Code Breakdown

  1. Header Inclusion:

C

#include <stdio.h>
  1. Main Function:

C

int main() {
  1. Variable Declaration:

C

int rows, i, j;
  1. User Input for Number of Rows:

C

printf("Enter the number of rows: ");
scanf("%d", &rows);
  1. Outer Loop for Rows:

C

for (i = 1; i <= rows; i++) {
  1. Inner Loop for Columns:

C

for (j = 1; j <= rows; j++) {
  1. Conditional Logic for Hollow Pattern:

C

    if (i == 1 || i == rows || j == 1 || j == rows) {
        printf("%d ", j); // Print numbers for outer edges
    } else {
        printf("  "); // Print spaces for inner area
    }
}
  1. Newline for Next Row:

C

printf("\n");
  1. Return Statement:

C

return 0;
}

Full Code:

C

#include <stdio.h>

int main() {
    int rows, i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= rows; j++) {
            if (i == 1 || i == rows || j == 1 || j == rows) {
                printf("%d ", j);
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. The stdio.h header provides input/output functions.
  2. The main function is the program’s entry point.
  3. Variables rows, i, and j track the number of rows, row counter, and column counter, respectively.
  4. The user specifies the desired number of rows.
  5. The outer loop iterates through each row of the square.
  6. The inner loop iterates through each column within a row.
  7. The conditional statement checks if a position belongs to the outer edges and prints the number; otherwise, it prints a space, creating the hollow effect.
  8. printf("\n") moves the cursor to the next line, starting a new row.
  9. return 0 indicates successful program execution.

Customization and Enhancements:

  • Change the starting number for different patterns.
  • Experiment with various number sequences.
  • Explore alternative characters for visual variation.

Conclusion:

By mastering loops, conditional statements, and number printing in C, you can create visually engaging patterns like the hollow square number pattern. Understanding the logic behind the code empowers you to experiment with different patterns and foster your programming creativity.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button