Hollow rectangle pattern
Table of Contents
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
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
Approach
Program 2
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
Approach
Program 3
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
Approach
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.