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