คำอธิบายโปรแกรม
ในเรขาคณิต สี่เหลี่ยมจัตุรัสเป็นรูปสี่เหลี่ยมสมมาตร ที่มีด้านเท่ากัน 4 ด้าน และมุมเท่ากัน 4 มุม
รูปสี่เหลี่ยมจัตุรัสทึบ (Solid) และโปร่ง (Hollow) จะมีลักษณะดังนี้:
อัลกอริทึม
สำหรับสี่เหลี่ยมทึบ (Solid Square) -
รับจำนวนแถวจากผู้ใช้ สำหรับแต่ละแถว พิมพ์ * สำหรับทุกคอลัมน์
สำหรับสี่เหลี่ยมโปร่ง (Hollow Square) -
รับจำนวนแถวจากผู้ใช้ สำหรับแถวแรกและแถวสุดท้าย พิมพ์ * ทุกคอลัมน์ สำหรับแถวกลาง พิมพ์ * เฉพาะคอลัมน์แรกและคอลัมน์สุดท้าย
ตัวอย่างโค้ด
/* Program to print hollow and Solid Square pattern */
#include <stdio.h>
int main() {
int r, c, rows;
int r1, c1, rows1;
// clrscr();
/* Hollow Square */
printf("Enter the Number of rows for Hollow Square: ");
scanf("%d", &rows);
printf("\n");
for (r = 1; r <= rows; r++) {
if (r == 1 || r == rows) {
for (c = 1; c <= rows; c++) {
printf("*");
}
} else {
for (c = 1; c <= rows; c++) {
if (c == 1 || c == rows) {
printf("*");
} else {
printf(" ");
}
}
}
printf("\n");
}
printf("\n");
/* Solid Square */
printf("Enter the Number of rows for Solid Square: ");
scanf("%d", &rows1);
printf("\n");
for (r1 = 1; r1 <= rows1; r1++) {
for (c1 = 1; c1 <= rows1; c1++) {
printf("*");
}
printf("\n");
}
// getch();
return 0;
}
เอาต์พุต
แสดงรูปสี่เหลี่ยมจัตุรัสทึบและโปร่ง ตามจำนวนแถวที่ป้อนเข้าไป