fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. int rows, cols;
  8. // Getting dimensions of rectangle.
  9. cout << "Enter the number of rows in rectangle - ";
  10. cin >> rows;
  11.  
  12. cout << "Enter the number of columns in rectangle - ";
  13. cin >> cols;
  14.  
  15. cout << "Rectangle of dimensions " << rows << "*" << cols << endl;
  16.  
  17. // Main logic to print hollow rectangle.
  18. for (int i = 0; i < rows; i++) {
  19. for (int j = 0; j < cols; j++) {
  20.  
  21. // If the index is at the border, then print *.
  22. if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1)
  23. cout << "* ";
  24. else
  25. cout << " ";
  26. }
  27. cout << endl;
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5280KB
stdin
7
5
7*5
stdout
Enter the number of rows in rectangle - Enter the number of columns in rectangle - Rectangle of dimensions 7*5
* * * * * 
*       * 
*       * 
*       * 
*       * 
*       * 
* * * * *