Hollow Square Star Pattern
Program to Print Hollow Square Star Pattern
PRINTING PATTERN:
***** * * * * *****
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
1. Take the number of rows/columns as input from the user (length of the square's side) and store it in a variable ('n').
2. Run a loop 'n' times to iterate through the rows (i=0 to i<n).
3. For the first and last rows, print stars for all columns.
4. For the rows in between, print a star at the beginning and end, and spaces in between.
5. Move to the next line after each row has been printed.
Code in C:
#include <stdio.h> int main() { int n, i, j; // Declaring integers n for size, i and j for loops printf("Enter the number of rows/columns for the hollow square:\n"); scanf("%d", &n); // Taking input for number of rows/columns for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // Print '*' for the first and last row, and first and last column if (i == 0 || i == n - 1 || j == 0 || j == n - 1) { printf("*"); } else { printf(" "); // Print space for hollow part } } printf("\n"); // New line after each row } return 0; }
Output:
Enter the number of rows/columns for the hollow square: 5 ***** * * * * *****