C++ program to print hollow inverted pyramid star pattern

Example

Input :

Input rows: 5

Output:

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

Required Knowledge

Basic C++ programming, if-else, for loop, nested loop

Program to print hollow inverted pyramid star pattern

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

#include <iostream>
using namespacce std;

int main()
{
     int i, j, rows;
     
     /* Input rows to print from user */
     cout<<"Enter number of rows: ";
     cin>>rows;

     /* Iterate through rows */
     for(i=1; i<=rows; i++)
     {
          /* Print leading spaces */
          for(j=1; j<i; j++)
          {
                cout<<" ";
          }

          /* Print hollow pyramid */
          for(j=1; j<=(rows*2 - (2*i-1)); j++)
          {
                /*
                     * Print star for first row(i==1),
                     * ith column (j==1) and for
                     * last column (rows*2-(2*i-1)) 
                */
                if(i==1 || j==1 || j==(rows*2 - (2*i - 1)))
                {
                     cout<<"*";
                }  
                else
                {
                     cout<<" ";
                }
         }

         /* Move to next line */
         cout<<"\n";
     }
     return 0;
 }
                    Output
Enter number of rows: 5
********
 *       *
   *    *
     * *
      *
  • Save

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap