Deitel C++ How To Program 9th Edition Chapter 4 Exercise 4.25

13 Jul 2024 - Syed Muhammad Shahrukh Hussain

Write a program which prints a hollow square using asterisks (*) with size of sides of square as input.

Terminal

Enter Size:4
****
*  *
*  *
****

Solution

#include <iostream>

using namespace std;

int main() {
  int size;

  cout << "Enter Size:";
  cin >> size;
  if (size <= 0 ||size > 20) {
    cout << "Please enter between [1-20]" << endl;
    return 0;
  }
  for (int i  = 0 ; i < size ; i++) {
    for (int j = 0; j < size ; j++) {
      if (i == 0 || i == (size -1)) {
        cout <<  "*";
      } else if (j == 0 || j == (size -1)) {
        cout  << "*";
      } else {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}

Sources