C Online Compiler
Example: Hollow Rectangle in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Hollow Rectangle #include <stdio.h> int main() { // Step 1: Declare variables for rows and columns int numRows, numCols; // Step 2: Prompt user for input printf("Enter the number of rows: "); scanf("%d", &numRows); printf("Enter the number of columns: "); scanf("%d", &numCols); // Step 3: Input validation if (numRows <= 0 || numCols <= 0) { printf("Dimensions must be positive integers.\n"); return 1; } // Special case for very small rectangles that cannot be hollow if (numRows < 2 || numCols < 2) { printf("Minimum dimensions for a hollow rectangle are 2x2. Printing solid instead.\n"); // Fallback to solid rectangle logic for small inputs if desired, or exit. // For this example, we'll still try to print based on the logic, which will be solid. } printf("\nYour hollow rectangle:\n"); // Step 4: Outer loop for rows for (int i = 0; i < numRows; i++) { // Step 5: Inner loop for columns for (int j = 0; j < numCols; j++) { // Step 6: Conditional check to print '*' or ' ' if (i == 0 || i == numRows - 1 || j == 0 || j == numCols - 1) { printf("*"); // Print '*' for border (first/last row, first/last column) } else { printf(" "); // Print ' ' for interior } } printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS