Answer to Question #263568 in C++ for john

Question #263568

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.


1
Expert's answer
2021-11-09T17:47:04-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS