C Online Compiler
Example: Box with User-Defined Dimensions in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Box with User-Defined Dimensions #include <stdio.h> int main() { // Step 1: Declare variables for width and height int width, height; // Step 2: Prompt user for dimensions and read input printf("Enter the width of the box: "); scanf("%d", &width); printf("Enter the height of the box: "); scanf("%d", &height); // Step 3: Validate input to ensure reasonable dimensions if (width < 3 || height < 3) { // Minimum size for a recognizable box printf("Dimensions must be at least 3 for a proper box outline.\n"); return 1; // Indicate an error } // Step 4: Loop through each row for (int i = 1; i <= height; i++) { // Step 5: Loop through each column in the current row for (int j = 1; j <= width; j++) { // Step 6: Check if current position is part of the border if (i == 1 || i == height || j == 1 || j == width) { printf("*"); // Print asterisk for border } else { printf(" "); // Print space for interior } } printf("\n"); // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS