C (programming language)

C Program To Print Square star Pattern

Shining Bright: Creating Square Star Patterns in C

The humble asterisk (*), when wielded with the power of loops and logic, can transform into dazzling star patterns on your screen. This guide delves into crafting a C program that prints stunning square star patterns of any desired size.

Building Blocks: Loops and Logic

Our star-spangled journey begins with two fundamental tools:

  • Loops: We leverage nested loops to iterate through rows and columns, meticulously positioning our * characters.
  • Conditional statements: These act as guiding lights, determining when to print a star and when to leave a space, shaping the desired square format.

Step 1: Setting the Stage

C

#include <stdio.h>

int size; // Variable to store user-defined pattern size

We include the stdio.h library for user interaction and declare an integer variable size to hold the desired pattern size.

Step 2: User Input

C

printf("Enter the size of the square star pattern: ");
scanf("%d", &size);

Prompt the user to enter the desired size of the square pattern, accepting the input as an integer and storing it in size.

Step 3: Outer Loop – Row Iteration

C

for (int i = 1; i <= size; i++) { // Loop iterates 'size' times, for each row
  // ...
}

Nested within this loop lies the inner loop that controls star placement within each row.

Step 4: Inner Loop – Column Iteration and Star Placement

C

for (int j = 1; j <= size; j++) { // Loop iterates 'size' times, for each column
  if (i == 1 || i == size || j == 1 || j == size) { // Conditional for printing stars
    printf("*");
  } else { // Conditional for printing spaces
    printf(" ");
  }
}
  • This loop iterates for each column within the current row.
  • The conditional statement checks if the current position is on the border (first or last row/column). If so, a star is printed.
  • Otherwise, a space is printed to maintain the square shape.

Step 5: Line Break and Loop Completion

C

printf("\n"); // Move to new line after completing each row
}
  • After iterating through all columns in a row, the printf("\n") statement adds a line break, moving to the next row for star placement.
  • The outer loop continues until all rows (specified by size) are completed.

Step 6: Witnessing the Stars Align

Compile and run the program! Enter the desired size, and behold the majestic square star pattern appear on your screen, a testament to your programming prowess.

Beyond the Basics: Expanding Your Starry Palette

  • Variable star character: Replace “*” with other characters like “+” or “#” for varied patterns.
  • Hollow Squares: Modify the conditionals to print stars only on borders and leave the inner space empty for a hollow square effect.
  • Diagonal patterns: Experiment with logic to print diagonal lines of stars within the square.

Keep exploring and experimenting! As you delve deeper into programming, remember that even the simplest tools, like loops and conditions, can become powerful brushes for painting dazzling patterns on the canvas of code.

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