Close
Close

how to draw a pattern of hollow diamond shape

   h_duddu

             the pattern is:

                               *

                            *    *

                         *         *

                      *               *

                             *   * 

                               *


Answers

  •   

    Let’s first print

       *   
      *    
     *     
    *      
     *     
      *    
       *  

    You can easily observe here that a star will get printed if the sum of line number and the position of start is 5 (upper half) or their difference is 3 (lower half).

    #include <stdio.h>
    
    int main()
    {
      int i,j;
      for(i=1; i<=7; i++)
      {
        for(j=1; j<=7; j++)
        {
          if((i-j==3) || (j+i==5))
            printf("*");
          else
            printf(" ");
        }
        printf("\n");
      }
      return 0;
    }
    

    Now after this you can observe that the sum of the positions of the stars at right and the stars at left is 8. So, just print a star at a position 8-position of the left star.

    #include <stdio.h>
    
    int main()
    {
      int i,j;
      for(i=1; i<=7; i++)
      {
        for(j=1; j<=7; j++)
        {
          if((i-j==3) || (j+i==5) || (i-(8-j)==3) || ((8-j)+i==5))
            printf("*");
          else
            printf(" ");
        }
        printf("\n");
      }
      return 0;
    }
    

    This is just one of the ways to print this pattern. You can think of many more ways.


    • Thank u very much sir!!!!!!!!!! I'm glad if u read this comment....sir,please add a online compiler in it and also python3.x
      - h_duddu

  •    emmausa

    I really like the information you share. I learned a lot of new and useful knowledge from your post. fnf



Ask Yours
Post Yours
Write your answer