Write a C++ Program to Print Rectangle Star Pattern with an example. In this C++ rectangular star pattern, we used the if statement within the for loop. It helps to print a given character or * for the rectangle border and empty space for all the remaining places. We can call it a Hallow rectangle star.
#include<iostream> using namespace std; int main() { int i, j, rows, columns; char ch; cout << "\nPlease Enter the Total Number of Rectangle Rows = "; cin >> rows; cout << "\nPlease Enter the Total Number of Rectangle Columns = "; cin >> columns; cout << "\nPlease Enter Any Symbol to Print = "; cin >> ch; cout << "\n-----Rectangle Pattern-----\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= columns; j++) { if(i == 1 || i == rows || j == 1 || j == columns) { cout << ch; } else { cout << " "; } } cout << "\n"; } return 0; }
C++ Program to Print Rectangle Star Pattern Example 2
In this C++ example, we removed the If statement to print a complete rectangle of stars.
#include<iostream> using namespace std; int main() { int i, j, rows, columns; char ch; cout << "\nPlease Enter the Total Number of Rectangle Rows = "; cin >> rows; cout << "\nPlease Enter the Total Number of Rectangle Columns = "; cin >> columns; cout << "\nPlease Enter Any Symbol to Print = "; cin >> ch; cout << "\n-----Rectangle Pattern-----\n"; for(i = 0; i < rows; i++) { for(j = 0; j < columns; j++) { cout << ch; } cout << "\n"; } return 0; }
Please Enter the Total Number of Rectangle Rows = 8
Please Enter the Total Number of Rectangle Columns = 20
Please Enter Any Symbol to Print = $
-----Rectangle Pattern-----
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
C++ Program to Print Rectangle Star Pattern using a While loop
#include<iostream> using namespace std; int main() { int i, j, rows, columns; char ch; cout << "\nPlease Enter the Total Number of Rectangle Rows = "; cin >> rows; cout << "\nPlease Enter the Total Number of Rectangle Columns = "; cin >> columns; cout << "\nPlease Enter Any Symbol to Print = "; cin >> ch; cout << "\n-----Rectangle Pattern-----\n"; i = 0; while(i < rows) { j = 0; while(j < columns) { cout << ch; j++; } cout << "\n"; i++; } return 0; }
Please Enter the Total Number of Rectangle Rows = 10
Please Enter the Total Number of Rectangle Columns = 22
Please Enter Any Symbol to Print = @
-----Rectangle Pattern-----
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@