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>
int main()
{
int hight;
int width;
std::cout << "Enter the height of the rectangle: ";
std::cin >> hight;
std::cout << "Enter the width of the rectangle: ";
std::cin >> width;
// Checking width is greater than height
if (hight > width)
{
int temp = hight;
hight = width;
width = temp;
}
for (int i = 1; i <= hight* width; i++ )
{
if (i <= width || i > width * (hight - 1) || i % width < 2)
{
std::cout << "*";
}
else
{
std::cout << " ";
}
if (i % width == 0)
{
std::cout << "\n";
}
}
return 0;
}
Comments
Leave a comment