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