Program to print a rectangle pattern in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print a given rectangular pattern.

For this we will be given with the height and the breath of the rectangle. Our task is to print the rectangle with the given dimensions using the “@” character.

Example

 Live Demo

#include<iostream>
using namespace std;
void print_rec(int h, int w){
   for (int i=0; i<h; i++){
      cout << "\n";
      for (int j=0; j<w; j++){
         if (i == 0 || i == h-1 ||
         j== 0 || j == w-1)
            cout << "@";
         else
            cout << " ";
      }
   }
}
int main(){
   int h = 5, w = 4;
   print_rec(h, w);
   return 0;
}

Output

@@@@
@  @
@  @
@  @
@@@@
raja
Published on 19-Dec-2019 15:00:40
Advertisements