C++ program to print hollow mirrored inverted right triangle star pattern

Example

Input :

 Input rows: 5 

Output :

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

Required Knowledge

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

Program to print hollow mirrored inverted right triangle star pattern

include <iostream>
using namespace std;

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

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

        // Print hollow inverted right triangle
        for(j=i; j<=rows; j++)
        {
             /*
                * Print star for ith column(j==i),
                * last column(j==rows) and for 
                * first row(i==1).
             */
             if(j==i || j==rows || i==1)
             {
                    cout<<"*";
             }
             else
             {
                    cout<<" ";
             }
        }
        cout<<"\n";
    }
    return 0;
}
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