Write a program that prompts the user to input height and width and print the hollow rectangle
as given in the output below. The program should check whether the both height and width
of rectangle are equal or not. Do not use nested loops.
#include <iostream>
using namespace std;
int main()
{
int height;
int width;
cout<<"\nEnter height of the rectangle: ";
cin>>height;
cout<<"\Enter width of the rectangle: ";
cin>>width;
for (int i = 0; i <height; i++)
{
for (int j = 0; j < width; j++)
{
if (i==0 || i==height-1 || j==0 || j==width-1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Comments
Leave a comment