C++ Program to Draw Rectangle using For loop
ADVERTISEMENTS
C++ program to draw rectangle using for loop. In this article, you will learn how to draw a rectangle in c++ using the for loop.
Source Code
// C++ Program to Draw Rectangle using For loop
#include <iostream>
using namespace std;
int main() {
int r = 0, c = 0, i, j;
// r - denotes the number of rows
// c - denotes the number of columns
cout << "-----Enter the number of rows & columns-----\n";
cin >> r >> c;
for (i = 1; i <= r; i++) {
for (j = 1; j <= c; j++) {
if (i == 1 || i == r || j == 1 || j == c)
cout << "* ";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output
-----Enter the number of rows & columns-----
8
12
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *
8
12
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *