ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมสำหรับพิมพ์รูปแบบที่น่าสนใจ
สำหรับสิ่งนี้เราจะได้ครึ่งความกว้างของลวดลาย งานของเราคือพิมพ์ลวดลายที่คล้ายคลึงกันตามความกว้างที่กำหนดโดยให้ส่วนซ้ายและขวาเป็นภาพสะท้อนของกันและกัน
ตัวอย่าง
#include<stdio.h>
//printing the given pattern
void print_pattern(int n){
int i,j;
//printing the upper half
for (i=1; i<=n; i++){
for (j=1; j<=(2*n); j++){
// Left portion
if (i<j)
printf(" ");
else
printf("*");
// Right portion
if (i<=((2*n)-j))
printf(" ");
else
printf("*");
}
printf("\n");
}
//printing the lower half
for (i=1; i<=n; i++){
for (j=1;j<=(2*n);j++){
// Left portion
if (i>(n-j+1))
printf(" ");
else
printf("*");
// Right portion
if ((i+n)>j)
printf(" ");
else
printf("*");
}
printf("\n");
}
}
int main(){
print_pattern(6);
return 0;
} ผลลัพธ์
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *