How to print hollow rectangle star pattern in C++

1 Answer

0 votes
#include <iostream>

void print_hollow_rectangle(int total_stars) {
   for (int i = 1; i <= total_stars; i++) {
      for (int j = 1; j <= total_stars; j++) {
         if (i == 1 || i == total_stars || j == 1 || j == total_stars) {
            std::cout << "*";
         }
         else {
            std::cout << " ";
         }
      }
      std::cout << "\n";
   } 
}
 
int main() {
   print_hollow_rectangle(7);
}
 
 
 
 
/*
run:
   
*******
*     *
*     *
*     *
*     *
*     *
*******
   
*/

 



answered Sep 22, 2021 by avibootz
edited Sep 11, 2023 by avibootz
...