C program to print hollow inverted pyramid star pattern

Write a C program print hollow inverted pyramid star(*) pattern series of n rows using for loop. How to print hollow inverted equilateral triangle star pattern series of n rows using for loop in C programming. Logic to print hollow inverted pyramid star pattern in C program.

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Must know -

Logic to print hollow pyramid star pattern

If you look to the pattern structure you will notice that you have to do two things with this pattern. First is you have to print a simple inverted pyramid and second is you have to make the inverted pyramid hollow. Printing inverted pyramid won't be difficult as I discussed in my previous post. To make pyramid hollow you just have to apply a condition before printing stars i.e. print stars only when row=1 or column=1 or column=(2*rownumber - 1) otherwise print space.

Program to print hollow pyramid star pattern

/**
 * C program to print hollow inverted pyramid star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, n;

    // Input rows to print from user
    printf("Enter value of n : ");
    scanf("%d", &n);

    for(i=n; i>=1; i--)
    {
        // Print trailing spaces
        for(j=i; j<n; j++)
        {
            printf(" ");
        }

        // Print hollow pyramid
        for(j=1; j<=(2*i-1); j++)
        {
            if(i==n || j==1 || j==(2*i-1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
*********
 *     *
  *   *
   * *
    *

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>