Program to print Interesting pattern in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print a given interesting pattern.

For this, we will be provided with the half-width of the pattern. Our task is to print a similar pattern according to the given width with its left and right portions being mirror images of one another.

Example

 Live Demo

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

Output

*                     *
* *                 * *
* * *             * * *
* * * *         * * * *
* * * * *     * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * *     * * * * *
* * * *         * * * *
* * *             * * *
* *                 * *
*                     *
raja
Published on 02-Jan-2020 09:55:30
Advertisements