Hollow Rectangle Star Pattern
Program to Print Hollow Rectangle Star Pattern
PRINTING PATTERN:
****** * * * * ******
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
1. Take the number of rows and columns as input from the user and store them in variables ('rows' and 'cols').
2. Run a loop 'rows' times to iterate through the rows (i=0 to i<rows).
3. Inside the outer loop, check if the current row is the first or last row. If so, print '*' for all columns.
4. For the rows in between, print '*' 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 rows, cols, i, j; // Declaring integers for rows, columns, and loop counters printf("Enter the number of rows for the hollow rectangle:\n"); scanf("%d", &rows); // Taking input for number of rows printf("Enter the number of columns for the hollow rectangle:\n"); scanf("%d", &cols); // Taking input for number of columns for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { // Print '*' for the first and last row, and first and last column if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) { printf("*"); } else { printf(" "); // Print space for hollow part } } printf("\n"); // New line after each row } return 0; }
Output:
Enter the number of rows for the hollow rectangle: 4 Enter the number of columns for the hollow rectangle: 6 ****** * * * * ******