Codeforcoding

Display Hollow rectangle and square star pattern in C++

Display Hollow rectangle and square star pattern in C++

In this article, we will discuss the concept of  the Program to Display Hollow rectangle or square star pattern in C++ programming language

Display Hollow rectangle and square star pattern in C++
Display Hollow pattern

In this post, we are going to learn how to write  Hollow rectangle or square star pattern using for loop, while loop and Do-while loop with example programs

 

Program 1

Print Hollow rectangle or square star pattern

Using for loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle or square pattern using for loop in C++ language

 

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int rows,columns,i,j;
    cout<<"Enter the number of rows\n";
   cin>>rows;//Takes input from user for rows
    cout<<"Enter the number of columns\n";
    cin>>columns;//Takes input from user for columns

    for (i=1; i<=rows; i++){
    for (j=1; j<=columns; j++){
    if(i==1||i==rows||j==1||j==columns){
        cout<<"*";
    }else{
        cout<<" ";
    }

}
    cout<<"\n";
}
getch();
    return 0;
}

When the above code is executed, it produces the following results

Output
Hollow rectangle and square star pattern in C++

Program 2

Using while loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle and square pattern using while loop in C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int rows,columns,i,j;
    cout<<"Enter the number of rows\n";
   cin>>rows;
    cout<<"Enter the number of columns\n";
    cin>>columns;
  i=1;
    while(i<=rows){
            j=1;
    while( j<=columns){
    if(i==1||i==rows||j==1||j==columns){
        cout<<"*";
    }else{
        cout<<" ";
    }
j++;
}
    cout<<"\n";
    i++;
}
getch();
    return 0;
}


When the above code is executed, it produces the following results

Output
Hollow rectangle and square star pattern in C++

 

Program 3

Using the do-while loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle and square pattern using the do-while loop in C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int rows,columns,i,j;
    cout<<"Enter the number of rows\n";
    cin>>rows;
    cout<<"Enter the number of columns\n";
    cin>>columns;

    i=1;
    do{
            j=1;
    do{
    if(i==1||i==rows||j==1||j==columns){
        cout<<"*";
    }else{
        cout<<" ";
    }
 j++;
}while(j<=columns);
    cout<<"\n";
     i++;
}while(i<=rows);
getch();
    return 0;
}


When the above code is executed, it produces the following results

Output
Hollow rectangle and square star pattern in C++

 

Suggested for you

If statement in C++ language

For loop in C++ language

Nested For loop in C++ language

while loop in C++ language

Nested while  loop in C++ language

Do-while loop in C++ language

Nested Do-while loop in C++ language

The operator in C++ language

Datatype in C++ language

Variable in C++ language

 

Similar post

C  code to Print Hollow rectangle and square star pattern

Java code to Print Hollow rectangle and square star pattern

C  code to print Hollow Pyramid star pattern

Java code to print Hollow Pyramid and square star pattern

C++  code to print Hollow Pyramid and square star pattern

 

Program to display combined Pyramid pattern in C using while loop
Print Hollow rectangle and square star pattern in C
Exit mobile version