c - drawing a rectangle for user-defined dimensions using for lops, using extended ASCII characters

C - drawing a rectangle for user-defined dimensions using for lops, using extended ASCII characters

To draw a rectangle using extended ASCII characters in C, you can use loops to print the rectangle's border and fill. Extended ASCII characters like #, *, or - are commonly used for this purpose.

Here's a simple C program that draws a rectangle based on user-defined dimensions using extended ASCII characters:

#include <stdio.h> int main() { int width, height; // Prompt user for dimensions printf("Enter width of the rectangle: "); scanf("%d", &width); printf("Enter height of the rectangle: "); scanf("%d", &height); // Validate dimensions if (width <= 0 || height <= 0) { printf("Width and height must be positive integers.\n"); return 1; } // Draw the top border of the rectangle for (int i = 0; i < width; i++) { printf("#"); } printf("\n"); // Draw the sides of the rectangle for (int i = 0; i < height - 2; i++) { printf("#"); // Left border for (int j = 0; j < width - 2; j++) { printf(" "); // Inside of the rectangle } printf("#\n"); // Right border } // Draw the bottom border of the rectangle for (int i = 0; i < width; i++) { printf("#"); } printf("\n"); return 0; } 

Explanation:

  1. Input Dimensions:

    • The program first prompts the user to input the width and height of the rectangle.
  2. Validation:

    • It checks if the input dimensions are positive integers.
  3. Drawing the Rectangle:

    • Top Border: The first loop prints the top border of the rectangle using #.
    • Sides: The second loop prints the sides of the rectangle. The left and right borders are printed with #, while the inside is filled with spaces.
    • Bottom Border: The last loop prints the bottom border of the rectangle.

Output Example:

If the user inputs 7 for width and 4 for height, the output will be:

####### # # # # ####### 

This code provides a basic way to draw rectangles with extended ASCII characters. You can customize the character used for drawing the rectangle by replacing # with any other extended ASCII character if desired.

Examples

  1. How to draw a rectangle using ASCII characters with user-defined dimensions in C?

    Description: Use nested for loops to print the rectangle using ASCII characters based on user-defined dimensions.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { printf("*"); // Draw border using asterisks } else { printf(" "); } } printf("\n"); } return 0; } 
  2. How to use extended ASCII characters to draw a filled rectangle in C?

    Description: Use extended ASCII characters (like ��) to create a filled rectangle with user-defined dimensions.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { printf("��"); // Draw filled rectangle using extended ASCII character } printf("\n"); } return 0; } 
  3. How to draw a rectangle with borders using ASCII characters in C?

    Description: Use + for corners, - for horizontal borders, and | for vertical borders.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1) { if (j == 0 || j == width - 1) { printf("+"); // Corners } else { printf("-"); // Horizontal borders } } else { if (j == 0 || j == width - 1) { printf("|"); // Vertical borders } else { printf(" "); } } } printf("\n"); } return 0; } 
  4. How to draw a hollow rectangle with user-defined dimensions in C?

    Description: Print only the borders of the rectangle, leaving the inside empty.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { printf("*"); // Border } else { printf(" "); // Inside } } printf("\n"); } return 0; } 
  5. How to draw a rectangle with diagonal lines using ASCII characters in C?

    Description: Use ASCII characters to draw a rectangle with diagonal lines from corners.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1 || i == j || i == height - j - 1) { printf("*"); // Borders and diagonals } else { printf(" "); } } printf("\n"); } return 0; } 
  6. How to create a rectangle with a specific character in C?

    Description: Allow the user to specify a character to use for drawing the rectangle.

    Code:

    #include <stdio.h> int main() { int width, height; char ch; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); printf("Enter character: "); scanf(" %c", &ch); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { printf("%c", ch); // Border } else { printf(" "); } } printf("\n"); } return 0; } 
  7. How to draw a rectangle with corners rounded using ASCII characters in C?

    Description: Use a combination of characters to simulate rounded corners in the rectangle.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if ((i == 0 && (j == 0 || j == width - 1)) || (i == height - 1 && (j == 0 || j == width - 1))) { printf("+"); // Corners } else if (i == 0 || i == height - 1) { printf("-"); // Horizontal borders } else if (j == 0 || j == width - 1) { printf("|"); // Vertical borders } else { printf(" "); } } printf("\n"); } return 0; } 
  8. How to draw a hollow rectangle with diagonal lines using ASCII in C?

    Description: Print only the borders and diagonal lines inside the rectangle.

    Code:

    #include <stdio.h> int main() { int width, height; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1 || i == j || i == height - j - 1) { printf("*"); // Borders and diagonals } else { printf(" "); } } printf("\n"); } return 0; } 
  9. How to draw a rectangle with different border and fill characters in C?

    Description: Use different characters for the border and the fill of the rectangle.

    Code:

    #include <stdio.h> int main() { int width, height; char border, fill; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); printf("Enter border character: "); scanf(" %c", &border); printf("Enter fill character: "); scanf(" %c", &fill); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { printf("%c", border); // Border } else { printf("%c", fill); // Fill } } printf("\n"); } return 0; } 
  10. How to draw a rectangle with variable border thickness in C?

    Description: Adjust the border thickness by printing multiple characters for each border position.

    Code:

    #include <stdio.h> int main() { int width, height, border_thickness; printf("Enter width: "); scanf("%d", &width); printf("Enter height: "); scanf("%d", &height); printf("Enter border thickness: "); scanf("%d", &border_thickness); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i < border_thickness || i >= height - border_thickness || j < border_thickness || j >= width - border_thickness) { printf("*"); // Border with thickness } else { printf(" "); } } printf("\n"); } return 0; } 

More Tags

babeljs shared eof lifecycle-hook google-cloud-dataflow kibana rxjs5 left-join loading dead-code

More Programming Questions

More Geometry Calculators

More Mortgage and Real Estate Calculators

More Electronics Circuits Calculators

More Housing Building Calculators