C++

Display Hollow square star pattern in C++ using loops

Display Hollow square star pattern in C++ using loops

In this tutorial, we will discuss the Display Hollow square star pattern in C++ using loops

We can display many types of number, Star, Alphabet patterns using for, while and do-while loop in C++ language

In this post, we are going to learn how to display Hollow square pattern  Using for, while and do-while loop in C++ language

Using for loop

This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using for loop in C++ language

Program 1

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

int main()
{
    int size,row,coloum;
    cout<<"Enter the size for Hollow rectangle\n";
    cin>>size;
    //get input from the user for num1
for (row=1; row<=size; row++){
  cout<<"*";
}
cout<<"\n";
for (coloum=1; coloum<=size-2; coloum++){
  for (row=1; row<=size; row++){
  if(row==1||row==size){
    cout<<"*";
  }else{
    cout<<" ";
  }

}
  cout<<"\n";
}
for(row=1; row<=size; row++){
  cout<<"*";
}
cout<<"\n";
getch();
    return 0;
}

When the above code is executed it produces the following output

Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first for loop from 1 to given size according to the loop structure for (row=1; row<=size; row++) to print star for first-line
  • To iterate through the column, run the outer for loop from 1 to given size according to the loop structure for (coloum=1; coloum<=size-2; coloum++)
  • Inner for loop print star when the if statement is satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally run the last for loop from 1 to given size according to the loop structure for(row=1; row<=size; row++) to print star for last line

 

Program 2

Using while loop

This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using while loop in C++ language

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

int main()
{
    int size,row,coloum;
    cout<<"Enter the size for Hollow rectangle\n";
    cin>>size;
    //get input from the user for num1
    row=1;
while( row<=size){
  cout<<"*";
  row++;
}
cout<<"\n";
coloum=1;
while(coloum<=size-2){
        row=1;
  while(row<=size){
  if(row==1||row==size){
    cout<<"*";
  }else{
    cout<<" ";
  }
row++;
}
  cout<<"\n";
  coloum++;
}
row=1;
while( row<=size){
  cout<<"*";
  row++;
}
cout<<"\n";
getch();
    return 0;
}

When the above code is executed it produces the following output

Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first for loop from 1 to given size according to the loop structure while( row<=size) to print star for first-line
  • To iterate through the column, run the outer for loop from 1 to given size according to the loop structure while(coloum<=size-2)
  • Inner while loop print star when the if statements satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally, run the last for loop from 1 to given size according to the loop structure while( row<=size) to print star for the last line

 

Program 3

Using the do-while loop

This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using the do-while loop in C++ language

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

int main()
{
    int size,row,coloum;
    cout<<"Enter the size for Hollow rectangle\n";
    cin>>size;
    //get input from the user for num1
    row=1;
do{
  cout<<"*";
  row++;
}while( row<=size);
cout<<"\n";
coloum=1;
do{
        row=1;
  do{
  if(row==1||row==size){
    cout<<"*";
  }else{
    cout<<" ";
  }
row++;
}while(row<=size);
  cout<<"\n";
  coloum++;
}while(coloum<=size-2);
row=1;
do{
  cout<<"*";
  row++;
}while( row<=size);
cout<<"\n";
getch();
    return 0;
}

When the above code is executed it produces the following output

Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first do-while loop from 1 to given size according to the loop structure while( row<=size) to print star for the first line
  • To iterate through the column, run the outer dowhile loop from 1 to given size according to the loop structure while(coloum<=size-2)
  • Inner do-while loop print star when the if statements satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally run the last do-while loop from 1 to given size according to the loop structure while( row<=size) to print star for the last line

 

Suggested for you
do-while in C++ language
Data type in C++ language

 

Similar post
Display Hollow rectangle pattern in Java using loops

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.