C Program to print hollow pyramid and diamond pattern

Here we will see how to generate hollow pyramid and diamond patterns using C. We can generate solid Pyramid patterns very easily. To make it hollow, we have to add some few tricks.

Hollow Pyramid

For the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.

Example Code

#include 
int main() {
   int n, i, j;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   for(i = 1; i");
   }
}

Output

Enter number of lines: 20
                                         *
                                       *   *
                                      *     *
                                     *       *
                                    *         *
                                   *           *
                                  *             *
                                 *               *
                                *                 *
                               *                   *
                              *                     *
                             *                       *
                            *                         *
                           *                           *
                          *                             *
                         *                               *
                        *                                 *
                       *                                   *
                      *                                     *
                     * * * * * * * * * * * * * * * *  * * *  * 

Hollow Diamond

For the diamond at the first line and at the last line it will print one star. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts. Diamond has two parts. The upper half and the lower half. At the upper half we have to increase the space count, and for the lower half, we have to decrease the space count. Here the line numbers can be divided into two parts by using another variable called mid.

Example Code

#include 
int main() {
   int n, i, j, mid;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   if(n %2 == 1) { //when n is odd, increase it by 1 to make it even
      n++;
}
mid = (n/2);
for(i = 1; i");
}
for(i = mid+1; i");
}

Output

Enter number of lines: 15
                      *
                    *   *
                  *       *
                 *         *
                *           *
               *             *
              *               *
             *                 *
              *               *
               *             *
                *           *
                 *         *
                   *     *
                    *   *
                      *
Updated on: 2019-07-30T22:30:25+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements