C++ program to print hollow square star pattern

Example

Input

     Enter number of rows: 5 

Output

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

Required Knowledge

Basic C++ programming, conditional statements, loops and nested loops

Program to print hollow square pattern

#include <iostream>
using namespace std;
     /*
          * Siddharth Goyal 
          * Chandigarh Engineering College, Landran
          *
     */
int main()
{
    int i, j, N;
    cout<<"Enter number of rows: ";
    cin>>N;

    for(i=1; i<=N; i++){
        for(j=1; j<=N; j++){
            if(i==1 || i==N || j==1 || j==N){
                cout<<("*");
            }
            else{
                cout<<" ";
            }
        }
        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