Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

흥미로운 패턴을 C++로 출력하는 프로그램

<시간/>

이 튜토리얼에서는 주어진 흥미로운 패턴을 인쇄하는 프로그램에 대해 논의할 것입니다.

이를 위해 패턴의 절반 너비가 제공됩니다. 우리의 임무는 왼쪽과 오른쪽 부분이 서로의 거울 이미지가 되도록 주어진 너비에 따라 유사한 패턴을 인쇄하는 것입니다.

예시

#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;
}

출력

*                     *
* *                 * *
* * *             * * *
* * * *         * * * *
* * * * *     * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * *     * * * * *
* * * *         * * * *
* * *             * * *
* *                 * *
*                     *